Skip to content

Commit

Permalink
[LTO][AIX] Invoking AIX System Assembler in LTO CodeGen
Browse files Browse the repository at this point in the history
This patch teaches LTOCodeGenerator to call into the AIX system assembler to generate object files. This is in contrast to the approach taken on other platforms, where the LTOCodeGenerate calls the integrated assembler to generate object files.  We need to rely on the system assembler because the integrated assembler is incomplete at the moment.

Reviewed By: w2yehia, MaskRay

Differential Revision: https://reviews.llvm.org/D134375
  • Loading branch information
qiongsiwu committed Sep 28, 2022
1 parent 96a79cb commit ef399d1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ struct LTOCodeGenerator {
bool determineTarget();
std::unique_ptr<TargetMachine> createTargetMachine();

bool useAIXSystemAssembler();
bool runAIXSystemAssembler(SmallString<128> &AssemblyFile);

void emitError(const std::string &ErrMsg);
void emitWarning(const std::string &ErrMsg);

Expand Down
64 changes: 64 additions & 0 deletions llvm/lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
Expand Down Expand Up @@ -114,6 +115,11 @@ cl::opt<std::string> LTOStatsFile(
"lto-stats-file",
cl::desc("Save statistics to the specified file"),
cl::Hidden);

cl::opt<std::string> AIXSystemAssemblerPath(
"lto-aix-system-assembler",
cl::desc("Absolute path to the system assembler, picked up on AIX only"),
cl::value_desc("path"));
}

LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
Expand Down Expand Up @@ -237,7 +243,61 @@ bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
return true;
}

bool LTOCodeGenerator::useAIXSystemAssembler() {
const auto &Triple = TargetMach->getTargetTriple();
return Triple.isOSAIX();
}

bool LTOCodeGenerator::runAIXSystemAssembler(SmallString<128> &AssemblyFile) {
assert(useAIXSystemAssembler() &&
"Runing AIX system assembler when integrated assembler is available!");

// Set the system assembler path.
std::string AssemblerPath(llvm::AIXSystemAssemblerPath.empty()
? "/usr/bin/as"
: llvm::AIXSystemAssemblerPath.c_str());

// Prepare inputs for the assember.
const auto &Triple = TargetMach->getTargetTriple();
const char *Arch = Triple.isArch64Bit() ? "-a64" : "-a32";
std::string ObjectFileName(AssemblyFile);
ObjectFileName[ObjectFileName.size() - 1] = 'o';
SmallVector<StringRef, 8> Args = {
"/bin/env", "LDR_CNTRL=MAXDATA32=0x80000000@${LDR_CNTRL}",
AssemblerPath, Arch,
"-many", "-o",
ObjectFileName, AssemblyFile};

// Invoke the assembler.
int RC = sys::ExecuteAndWait(Args[0], Args);

// Handle errors.
if (RC < -1) {
emitError("LTO assembler exited abnormally");
return false;
}
if (RC < 0) {
emitError("Unable to invoke LTO assembler");
return false;
}
if (RC > 0) {
emitError("LTO assembler invocation returned non-zero");
return false;
}

// Cleanup.
remove(AssemblyFile.c_str());

// Fix the output file name.
AssemblyFile = ObjectFileName;

return true;
}

bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
if (useAIXSystemAssembler())
setFileType(CGFT_AssemblyFile);

// make unique temp output file to put generated code
SmallString<128> Filename;

Expand Down Expand Up @@ -268,6 +328,10 @@ bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
else if (AreStatisticsEnabled())
PrintStatistics();

if (useAIXSystemAssembler())
if (!runAIXSystemAssembler(Filename))
return false;

NativeObjectPath = Filename.c_str();
*Name = NativeObjectPath.c_str();
return true;
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-lto/aix.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; REQUIRES: powerpc-registered-target
; REQUIRES: system-aix
; RUN: llvm-as < %s > %t1
; RUN: llvm-lto %t1 | FileCheck %s

Expand Down

0 comments on commit ef399d1

Please sign in to comment.