[llvm] ef399d1 - [LTO][AIX] Invoking AIX System Assembler in LTO CodeGen

Qiongsi Wu via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 28 11:27:29 PDT 2022


Author: Qiongsi Wu
Date: 2022-09-28T14:26:50-04:00
New Revision: ef399d1cfc6825fe11400e7886191c6f973aa089

URL: https://github.com/llvm/llvm-project/commit/ef399d1cfc6825fe11400e7886191c6f973aa089
DIFF: https://github.com/llvm/llvm-project/commit/ef399d1cfc6825fe11400e7886191c6f973aa089.diff

LOG: [LTO][AIX] Invoking AIX System Assembler in LTO CodeGen

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

Added: 
    

Modified: 
    llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
    llvm/lib/LTO/LTOCodeGenerator.cpp
    llvm/test/tools/llvm-lto/aix.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
index fdd6ec74bce6d..203f56d424a25 100644
--- a/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
+++ b/llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
@@ -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);
 

diff  --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 8c374e0f2f858..68ef8d60beac7 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -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"
@@ -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)
@@ -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;
 
@@ -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;

diff  --git a/llvm/test/tools/llvm-lto/aix.ll b/llvm/test/tools/llvm-lto/aix.ll
index 6efd98ed9f701..fa1c3b0503248 100644
--- a/llvm/test/tools/llvm-lto/aix.ll
+++ b/llvm/test/tools/llvm-lto/aix.ll
@@ -1,4 +1,4 @@
-; REQUIRES: powerpc-registered-target
+; REQUIRES: system-aix
 ; RUN: llvm-as < %s > %t1
 ; RUN: llvm-lto %t1 | FileCheck %s
 


        


More information about the llvm-commits mailing list