Skip to content

Commit 64573da

Browse files
authored
[IR] Add "Large Data Threshold" module metadata (#66797)
This allows us to not have to pass -mllvm flags to set the large data threshold for (in-LLD/not-distributed) ThinLTO. Follows https://reviews.llvm.org/D52322, which did the same for the code model. Since the large data threshold is tied to the code model and we disallow mixing different code models, do the same for the large data threshold.
1 parent 96ea48f commit 64573da

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

llvm/include/llvm/IR/Module.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,17 @@ class LLVM_EXTERNAL_VISIBILITY Module {
922922
void setCodeModel(CodeModel::Model CL);
923923
/// @}
924924

925+
/// @}
926+
/// @name Utility function for querying and setting the large data threshold
927+
/// @{
928+
929+
/// Returns the code model (tiny, small, kernel, medium or large model)
930+
std::optional<uint64_t> getLargeDataThreshold() const;
931+
932+
/// Set the code model (tiny, small, kernel, medium or large)
933+
void setLargeDataThreshold(uint64_t Threshold);
934+
/// @}
935+
925936
/// @name Utility functions for querying and setting PGO summary
926937
/// @{
927938

llvm/lib/IR/Module.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,23 @@ void Module::setCodeModel(CodeModel::Model CL) {
631631
addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
632632
}
633633

634+
std::optional<uint64_t> Module::getLargeDataThreshold() const {
635+
auto *Val =
636+
cast_or_null<ConstantAsMetadata>(getModuleFlag("Large Data Threshold"));
637+
638+
if (!Val)
639+
return std::nullopt;
640+
641+
return cast<ConstantInt>(Val->getValue())->getZExtValue();
642+
}
643+
644+
void Module::setLargeDataThreshold(uint64_t Threshold) {
645+
// Since the large data threshold goes along with the code model, the merge
646+
// behavior is the same.
647+
addModuleFlag(ModFlagBehavior::Error, "Large Data Threshold",
648+
ConstantInt::get(Type::getInt64Ty(Context), Threshold));
649+
}
650+
634651
void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) {
635652
if (Kind == ProfileSummary::PSK_CSInstr)
636653
setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,12 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
225225
std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
226226
TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
227227
CodeModel, Conf.CGOptLevel));
228+
228229
assert(TM && "Failed to create target machine");
230+
231+
if (std::optional<uint64_t> LargeDataThreshold = M.getLargeDataThreshold())
232+
TM->setLargeDataThreshold(*LargeDataThreshold);
233+
229234
return TM;
230235
}
231236

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target triple = "x86_64-unknown-linux-gnu"
2+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
3+
4+
define void @bar() {
5+
ret void
6+
}
7+
!llvm.module.flags = !{!0, !1}
8+
9+
!0 = !{i32 1, !"Code Model", i32 3}
10+
!1 = !{i32 1, !"Large Data Threshold", i32 101}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; RUN: llvm-as %s -o %t.o
2+
; RUN: llvm-lto2 run -r %t.o,_start,px %t.o -o %t.s
3+
; RUN: llvm-objdump -d %t.s.0 | FileCheck %s
4+
5+
target triple = "x86_64-unknown-linux-gnu"
6+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
7+
8+
@data = internal constant [20 x i8] zeroinitializer
9+
10+
define ptr @_start() {
11+
entry:
12+
; CHECK-LABEL: <_start>:
13+
; CHECK: leaq (%rip), %rax
14+
; CHECK-NOT: movabsq
15+
ret ptr @data
16+
}
17+
18+
!llvm.module.flags = !{!0, !1}
19+
20+
!0 = !{i32 1, !"Code Model", i32 3}
21+
!1 = !{i32 1, !"Large Data Threshold", i32 100}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llvm-as %s -o %t.o
2+
; RUN: llvm-lto2 run -r %t.o,_start,px %t.o -o %t.s
3+
; RUN: llvm-objdump -d %t.s.0 | FileCheck %s
4+
5+
target triple = "x86_64-unknown-linux-gnu"
6+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
7+
8+
@data = internal constant [20 x i8] zeroinitializer
9+
10+
define ptr @_start() {
11+
entry:
12+
; CHECK-LABEL: <_start>:
13+
; CHECK: movabsq $0x0, %rax
14+
ret ptr @data
15+
}
16+
17+
!llvm.module.flags = !{!0, !1}
18+
19+
!0 = !{i32 1, !"Code Model", i32 3}
20+
!1 = !{i32 1, !"Large Data Threshold", i32 10}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llvm-as %s -o %t0.o
2+
; RUN: llvm-as < %p/Inputs/largedatathreshold.ll > %t1.o
3+
; RUN: not llvm-lto2 run -r %t0.o,_start,px -r %t1.o,bar,px %t0.o %t1.o -o %t2.s 2>&1 | FileCheck %s
4+
5+
; CHECK: 'Large Data Threshold': IDs have conflicting values
6+
7+
target triple = "x86_64-unknown-linux-gnu"
8+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
9+
10+
@data = internal constant [20 x i8] zeroinitializer
11+
12+
define ptr @_start() {
13+
entry:
14+
ret ptr @data
15+
}
16+
17+
!llvm.module.flags = !{!0, !1}
18+
19+
!0 = !{i32 1, !"Code Model", i32 3}
20+
!1 = !{i32 1, !"Large Data Threshold", i32 100}

0 commit comments

Comments
 (0)