[llvm] [Legacy ThinLTO] Use code model from IR when creating TargetMachine (PR #113617)

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 24 13:32:57 PDT 2024


https://github.com/hubert-reinterpretcast created https://github.com/llvm/llvm-project/pull/113617

It is observed that libLTO.so, which uses the legacy ThinLTO interface
respects neither the `--code-model=` option nor the code model encoded
in the IR.

This PR is a fix to respect the code model encoded in the IR.


>From 30d639af8f3708b88919deb978832f28b70fb09f Mon Sep 17 00:00:00 2001
From: Hubert Tong <hubert.reinterpretcast at gmail.com>
Date: Thu, 24 Oct 2024 16:24:13 -0400
Subject: [PATCH 1/2] [Legacy ThinLTO] Use code model from IR when creating
 TargetMachine

It is observed that libLTO.so, which uses the legacy ThinLTO interface
respects neither the `--code-model=` option nor the code model encoded
in the IR.

This PR is a fix to respect the code model encoded in the IR.
---
 .../llvm/LTO/legacy/ThinLTOCodeGenerator.h    |  2 +-
 llvm/lib/LTO/ThinLTOCodeGenerator.cpp         | 20 ++++++++++---------
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h b/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
index 676d4e179bed4b..5f8e76001f610d 100644
--- a/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
+++ b/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
@@ -47,7 +47,7 @@ struct ThinLTOCodeGeneratorImpl::TargetMachineBuilder {
   std::optional<Reloc::Model> RelocModel;
   CodeGenOptLevel CGOptLevel = CodeGenOptLevel::Aggressive;
 
-  std::unique_ptr<TargetMachine> create() const;
+  std::unique_ptr<TargetMachine> create(Module &) const;
 };
 
 /// This class define an interface similar to the LTOCodeGenerator, but adapted
diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index 4522f4adcebe68..18efae396fa44d 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -576,7 +576,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
 }
 
 // TargetMachine factory
-std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
+std::unique_ptr<TargetMachine>
+TargetMachineBuilder::create(Module &TheModule) const {
   std::string ErrMsg;
   const Target *TheTarget =
       TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -589,9 +590,9 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
   Features.getDefaultSubtargetFeatures(TheTriple);
   std::string FeatureStr = Features.getString();
 
-  std::unique_ptr<TargetMachine> TM(
-      TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
-                                     RelocModel, std::nullopt, CGOptLevel));
+  std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
+      TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
+      TheModule.getCodeModel(), CGOptLevel));
   assert(TM && "Cannot create target machine");
 
   return TM;
@@ -913,8 +914,8 @@ void ThinLTOCodeGenerator::optimize(Module &TheModule) {
   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
 
   // Optimize now
-  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
-                 DebugPassManager, nullptr);
+  optimizeModule(TheModule, *TMBuilder.create(TheModule), OptLevel,
+                 Freestanding, DebugPassManager, nullptr);
 }
 
 /// Write out the generated object file, either from CacheEntryPath or from
@@ -990,7 +991,8 @@ void ThinLTOCodeGenerator::run() {
                                              /*IsImporting*/ false);
 
         // CodeGen
-        auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
+        auto OutputBuffer =
+            codegenModule(*TheModule, *TMBuilder.create(*TheModule));
         if (SavedObjectsDirectoryPath.empty())
           ProducedBinaries[count] = std::move(OutputBuffer);
         else
@@ -1177,8 +1179,8 @@ void ThinLTOCodeGenerator::run() {
         auto &ImportList = ImportLists[ModuleIdentifier];
         // Run the main process now, and generates a binary
         auto OutputBuffer = ProcessThinLTOModule(
-            *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
-            ExportList, GUIDPreservedSymbols,
+            *TheModule, *Index, ModuleMap, *TMBuilder.create(*TheModule),
+            ImportList, ExportList, GUIDPreservedSymbols,
             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
             DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
             DebugPassManager);

>From 9420bd58b53824cb6ab30fa4a5cf420ec8b1065c Mon Sep 17 00:00:00 2001
From: Hubert Tong <hubert.reinterpretcast at gmail.com>
Date: Thu, 24 Oct 2024 16:25:33 -0400
Subject: [PATCH 2/2] Legacy ThinLTO LIT tests: codegen using code model from
 IR

---
 .../LTO/PowerPC/codemodel-1.legacy.thinlto.ll | 22 +++++++++++++++++
 .../LTO/PowerPC/codemodel-2.legacy.thinlto.ll | 24 +++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 llvm/test/LTO/PowerPC/codemodel-1.legacy.thinlto.ll
 create mode 100644 llvm/test/LTO/PowerPC/codemodel-2.legacy.thinlto.ll

diff --git a/llvm/test/LTO/PowerPC/codemodel-1.legacy.thinlto.ll b/llvm/test/LTO/PowerPC/codemodel-1.legacy.thinlto.ll
new file mode 100644
index 00000000000000..58ccfed2b2e7bb
--- /dev/null
+++ b/llvm/test/LTO/PowerPC/codemodel-1.legacy.thinlto.ll
@@ -0,0 +1,22 @@
+; RUN: llvm-as %s -o %t.o
+; RUN: llvm-lto --thinlto-action=run %t.o
+; RUN: llvm-objdump --disassemble-symbols=._start %t.o.thinlto.o | FileCheck %s --check-prefix=CHECK-SMALL
+
+target datalayout = "E-m:a-Fi64-i64:64-n32:64-S128-v256:256:256-v512:512:512"
+target triple = "powerpc64-ibm-aix7.2.0.0"
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 1, !"Code Model", i32 1}
+!3 = !{i32 1, !"EnableSplitLTOUnit", i32 0}
+
+ at data = internal constant [0 x i32] []
+
+define ptr @_start() nounwind readonly {
+entry:
+; CHECK-SMALL-LABEL:  <._start>:
+; CHECK-SMALL-NEXT: ld 3, 0(2)
+    ret ptr @data
+}
diff --git a/llvm/test/LTO/PowerPC/codemodel-2.legacy.thinlto.ll b/llvm/test/LTO/PowerPC/codemodel-2.legacy.thinlto.ll
new file mode 100644
index 00000000000000..e80550307ef678
--- /dev/null
+++ b/llvm/test/LTO/PowerPC/codemodel-2.legacy.thinlto.ll
@@ -0,0 +1,24 @@
+; RUN: llvm-as %s -o %t.o
+; RUN: llvm-lto --thinlto-action=run %t.o
+; RUN: llvm-objdump --disassemble-symbols=._start %t.o.thinlto.o | FileCheck %s --check-prefix=CHECK-LARGE
+
+target datalayout = "E-m:a-Fi64-i64:64-n32:64-S128-v256:256:256-v512:512:512"
+target triple = "powerpc64-ibm-aix7.2.0.0"
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 1, !"Code Model", i32 4}
+!3 = !{i32 1, !"EnableSplitLTOUnit", i32 0}
+
+ at data = internal constant [0 x i32] []
+
+define ptr @_start() nounwind readonly {
+entry:
+; CHECK-LARGE-LABEL:  <._start>:
+; CHECK-LARGE-NEXT: addis 3, 2, 0
+; CHECK-LARGE-NEXT: ld 3, 0(3)
+
+    ret ptr @data
+}



More information about the llvm-commits mailing list