Skip to content

Commit

Permalink
Merge pull request #51 from david-salinas/duplicate-symbols
Browse files Browse the repository at this point in the history
SWDEV-362823 - remove code to extract hip-clang bundles from .a files
  • Loading branch information
david-salinas authored Dec 1, 2022
2 parents 359504a + cb0ff86 commit 43b4672
Showing 1 changed file with 1 addition and 178 deletions.
179 changes: 1 addition & 178 deletions amd/hipcc/src/hipBin_amd.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,184 +638,7 @@ void HipBinAmd::executeHipCCCmd(vector<string> argv) {
swallowArg = 1;
}

// process linker response file for hip-clang
// extract object files from static library and pass them directly to
// hip-clang in command line.
// TODO(hipcc): Remove this after hip-clang switch to lto and lld is able to
// handle clang-offload-bundler bundles.
if ((hipBinUtilPtr_->stringRegexMatch(arg, "^-Wl,@.*")) ||
(hipBinUtilPtr_->stringRegexMatch(arg, "^@.*"))) {
// arg will have options type(-Wl,@ or @) and filename
vector<string> split_arg = hipBinUtilPtr_->splitStr(targetsStr, '@');
string file = split_arg.at(1);
ifstream in(file);
if (!in.is_open()) {
std::cerr << "unable to open file for reading: " << file << endl;
exit(-1);
}
string new_arg;
string tmpdir = hipBinUtilPtr_->getTempDir();
fs::path new_file = tmpdir;
new_file /= "response_file";
ofstream out(new_file);
if (!out.is_open()) {
std::cerr << "unable to open file for writing: " <<
new_file.string() << endl;
exit(-1);
}
string line;
while (getline(in, line)) {
line = hipBinUtilPtr_->trim(line);
if ((hipBinUtilPtr_->stringRegexMatch(line, ".*\\.a$")) ||
(hipBinUtilPtr_->stringRegexMatch(line, ".*\\.lo$"))) {
//## process static library for hip-clang
//## extract object files from static library and
//## pass them directly to hip-clang.
//## ToDo: Remove this after hip-clang switch to lto and
//## lld is able to handle clang-offload-bundler bundles.
string libFile = line;
string path = fs::absolute(line).string();
// Check if all files in .a are object files.
string cmd = "cd "+ tmpdir + "; ar xv " + path;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string cmdOut = sysOut.out;
vector<string> objs = hipBinUtilPtr_->splitStr(cmdOut, '\n');
bool allIsObj = 1;
string realObjs = "";
for (unsigned int i=0; i < objs.size(); i++) {
string obj = objs.at(i);
obj = hipBinUtilPtr_->trim(obj);
regex toReplace("x - ");
obj = hipBinUtilPtr_->replaceRegex(obj, toReplace, "");
obj = "\"" + tmpdir + "/" + obj;
cmd = "file " + obj;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string fileType = sysOut.out;
bool isObj;
(hipBinUtilPtr_->substringPresent(fileType, "ELF") ||
hipBinUtilPtr_->substringPresent(fileType, "COFF")) ?
isObj = true : isObj = false;
allIsObj = allIsObj && isObj;
if (isObj) {
realObjs = realObjs + " " + obj;
} else {
inputs.push_back(obj);
new_arg = "\"" + new_arg + obj + "\"";
}
} // end of objs for loop
realObjs = hipBinUtilPtr_->trim(realObjs);
if (allIsObj) {
out << line << "\n";
} else if (!realObjs.empty()) {
fs::path libFilefs = libFile;
string libBaseName = libFilefs.stem().string();
string libDir = libFilefs.parent_path().string();
string libExt = libFilefs.extension().string();
string libBaseNameTemp = libBaseName + "XXXXXX";
libBaseName = hipBinUtilPtr_->mktempFile(libBaseNameTemp) + libExt;
cmd = "cd " + tmpdir + "; ar rc " + libBaseName + " " +realObjs;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string cmdOut = sysOut.out;
out << tmpdir + "/"+ libBaseName + "\n";
}
} else if (hipBinUtilPtr_->stringRegexMatch(line, ".*\\.o$")) {
string cmd = "file " + line;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string fileType = sysOut.out;
bool isObj;
(hipBinUtilPtr_->substringPresent(fileType, "ELF") ||
hipBinUtilPtr_->substringPresent(fileType, "COFF")) ?
isObj = true : isObj = false;
if (isObj) {
out << line << "\n";
} else {
inputs.push_back(line);
new_arg = "\"" + new_arg + " " + line + "\"";
}
} else {
out << line << "\n";
}
} // end of while loop
in.close();
out.close();
arg = "\"" + new_arg +" " +split_arg.at(0) + "\\" + new_file.string();
escapeArg = 0;
} else if ((hipBinUtilPtr_->stringRegexMatch(arg, ".*\\.a$")) ||
(hipBinUtilPtr_->stringRegexMatch(arg, ".*\\.lo$"))) {
string new_arg = "";
string tmpdir = hipBinUtilPtr_->getTempDir();
string libFile = arg;
string path = fs::absolute(arg).string();
string cmd = "cd "+ tmpdir + "; ar xv " + path;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string cmdOut = sysOut.out;
vector<string> objs = hipBinUtilPtr_->splitStr(cmdOut, '\n');
bool allIsObj = 1;
string realObjs = "";
for (unsigned int i =0; i< objs.size(); i++) {
string obj = objs.at(i);
obj = hipBinUtilPtr_->trim(obj);
regex toReplace("x - ");
string replaceWith = "";
obj = hipBinUtilPtr_->replaceRegex(obj, toReplace , replaceWith);
obj = "\"" + tmpdir + "/" + obj + "\"";
string cmd = "file " + obj;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string fileType = sysOut.out;
bool isObj;
isObj = (hipBinUtilPtr_->substringPresent(fileType, "ELF") ||
hipBinUtilPtr_->substringPresent(fileType, "COFF"));
if (hipBinUtilPtr_->substringPresent(fileType, "ELF")) {
cmd = "llvm-readelf -e -W " + obj;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string sections = sysOut.out;
isObj = !(hipBinUtilPtr_->substringPresent(
sections, "__CLANG_OFFLOAD_BUNDLE__"));
}
allIsObj = (allIsObj && isObj);
if (isObj) {
realObjs = realObjs + " " + obj;
} else {
inputs.push_back(obj);
if (new_arg != "") {
new_arg += " ";
}
new_arg += "\"" + obj + "\"";
}
} // end of objs for loop

realObjs = hipBinUtilPtr_->trim(realObjs);
if (allIsObj) {
new_arg = arg;
} else if (!realObjs.empty()) {
fs::path libFilefs = libFile;
string libBaseName = libFilefs.stem().string();
string libDir = libFilefs.parent_path().string();
string libExt = libFilefs.extension().string();
string libBaseNameTemp = libBaseName + "XXXXXX";
libBaseName = hipBinUtilPtr_->mktempFile(
libBaseNameTemp) + libExt;
string cmd = "cd " + tmpdir +"; ar rc " +
libBaseName + " " + realObjs;
SystemCmdOut sysOut;
sysOut = hipBinUtilPtr_->exec(cmd.c_str());
string cmdOut = sysOut.out;
new_arg += "\"" + tmpdir +"/" + libBaseName + "\"";
}
arg = "\"" + new_arg + "\"";
escapeArg = 0;
if (hipBinUtilPtr_->stringRegexMatch(toolArgs, ".*-Xlinker$")) {
toolArgs = toolArgs.substr(0, -8);
toolArgs = hipBinUtilPtr_->trim(toolArgs);
}
} else if (arg == "-x") { // end of substring \.a || .lo section
if (arg == "-x") {
fileTypeFlag = 1;
} else if ((arg == "c" && prevArg == "-x") || (arg == "-xc")) {
fileTypeFlag = 1;
Expand Down

0 comments on commit 43b4672

Please sign in to comment.