Skip to content

Commit 5e0dd61

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:38e435895779 into amd-gfx:14c4d990c781
Local branch amd-gfx 14c4d99 Merged main:02cbae4fe068 into amd-gfx:5118390a1fcc Remote branch main 38e4358 [X86] With large code model, put functions into .ltext with large section flag (llvm#73037)
2 parents 14c4d99 + 38e4358 commit 5e0dd61

File tree

31 files changed

+482
-129
lines changed

31 files changed

+482
-129
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,14 +977,60 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
977977
return true;
978978
}
979979

980-
void tools::addFortranRuntimeLibs(const ToolChain &TC,
980+
void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
981981
llvm::opt::ArgStringList &CmdArgs) {
982982
// These are handled earlier on Windows by telling the frontend driver to add
983983
// the correct libraries to link against as dependents in the object file.
984984
if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
985+
// The --whole-archive option needs to be part of the link line to
986+
// make sure that the main() function from Fortran_main.a is pulled
987+
// in by the linker. Determine if --whole-archive is active when
988+
// flang will try to link Fortran_main.a. If it is, don't add the
989+
// --whole-archive flag to the link line. If it's not, add a proper
990+
// --whole-archive/--no-whole-archive bracket to the link line.
991+
bool WholeArchiveActive = false;
992+
for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA))
993+
if (Arg)
994+
for (StringRef ArgValue : Arg->getValues()) {
995+
if (ArgValue == "--whole-archive")
996+
WholeArchiveActive = true;
997+
if (ArgValue == "--no-whole-archive")
998+
WholeArchiveActive = false;
999+
}
1000+
1001+
if (!WholeArchiveActive)
1002+
CmdArgs.push_back("--whole-archive");
9851003
CmdArgs.push_back("-lFortran_main");
1004+
if (!WholeArchiveActive)
1005+
CmdArgs.push_back("--no-whole-archive");
1006+
1007+
// Perform regular linkage of the remaining runtime libraries.
9861008
CmdArgs.push_back("-lFortranRuntime");
9871009
CmdArgs.push_back("-lFortranDecimal");
1010+
} else {
1011+
unsigned RTOptionID = options::OPT__SLASH_MT;
1012+
if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
1013+
RTOptionID = llvm::StringSwitch<unsigned>(rtl->getValue())
1014+
.Case("static", options::OPT__SLASH_MT)
1015+
.Case("static_dbg", options::OPT__SLASH_MTd)
1016+
.Case("dll", options::OPT__SLASH_MD)
1017+
.Case("dll_dbg", options::OPT__SLASH_MDd)
1018+
.Default(options::OPT__SLASH_MT);
1019+
}
1020+
switch (RTOptionID) {
1021+
case options::OPT__SLASH_MT:
1022+
CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.static.lib");
1023+
break;
1024+
case options::OPT__SLASH_MTd:
1025+
CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.static_dbg.lib");
1026+
break;
1027+
case options::OPT__SLASH_MD:
1028+
CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.dynamic.lib");
1029+
break;
1030+
case options::OPT__SLASH_MDd:
1031+
CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.dynamic_dbg.lib");
1032+
break;
1033+
}
9881034
}
9891035
}
9901036

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
116116
bool IsOffloadingHost = false, bool GompNeedsRT = false);
117117

118118
/// Adds Fortran runtime libraries to \p CmdArgs.
119-
void addFortranRuntimeLibs(const ToolChain &TC,
119+
void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args,
120120
llvm::opt::ArgStringList &CmdArgs);
121121

122122
/// Adds the path for the Fortran runtime libraries to \p CmdArgs.

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
678678
// to generate executables.
679679
if (getToolChain().getDriver().IsFlangMode()) {
680680
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
681-
addFortranRuntimeLibs(getToolChain(), CmdArgs);
681+
addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
682682
}
683683

684684
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))

clang/lib/Driver/ToolChains/DragonFly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
153153
// AddRunTimeLibs).
154154
if (D.IsFlangMode()) {
155155
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
156-
addFortranRuntimeLibs(ToolChain, CmdArgs);
156+
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
157157
CmdArgs.push_back("-lm");
158158
}
159159

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
310310
// AddRunTimeLibs).
311311
if (D.IsFlangMode()) {
312312
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
313-
addFortranRuntimeLibs(ToolChain, CmdArgs);
313+
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
314314
if (Profiling)
315315
CmdArgs.push_back("-lm_p");
316316
else

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
564564
// AddRunTimeLibs).
565565
if (D.IsFlangMode()) {
566566
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
567-
addFortranRuntimeLibs(ToolChain, CmdArgs);
567+
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
568568
CmdArgs.push_back("-lm");
569569
}
570570

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
118118
// AddRunTimeLibs).
119119
if (D.IsFlangMode()) {
120120
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
121-
addFortranRuntimeLibs(ToolChain, CmdArgs);
121+
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
122122
}
123123

124124
CmdArgs.push_back("-lgcc");

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
131131

132132
if (C.getDriver().IsFlangMode()) {
133133
addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
134-
addFortranRuntimeLibs(TC, CmdArgs);
134+
addFortranRuntimeLibs(TC, Args, CmdArgs);
135135

136136
// Inform the MSVC linker that we're generating a console application, i.e.
137137
// one with `main` as the "user-defined" entry point. The `main` function is

clang/lib/Driver/ToolChains/MinGW.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
249249

250250
if (C.getDriver().IsFlangMode()) {
251251
addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
252-
addFortranRuntimeLibs(TC, CmdArgs);
252+
addFortranRuntimeLibs(TC, Args, CmdArgs);
253253
}
254254

255255
// TODO: Add profile stuff here

clang/lib/Driver/ToolChains/NetBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
325325
// AddRunTimeLibs).
326326
if (D.IsFlangMode()) {
327327
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
328-
addFortranRuntimeLibs(ToolChain, CmdArgs);
328+
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
329329
CmdArgs.push_back("-lm");
330330
}
331331

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
237237
// AddRunTimeLibs).
238238
if (D.IsFlangMode()) {
239239
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
240-
addFortranRuntimeLibs(ToolChain, CmdArgs);
240+
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
241241
if (Profiling)
242242
CmdArgs.push_back("-lm_p");
243243
else

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
226226
// these dependencies need to be listed before the C runtime below.
227227
if (D.IsFlangMode()) {
228228
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
229-
addFortranRuntimeLibs(getToolChain(), CmdArgs);
229+
addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
230230
CmdArgs.push_back("-lm");
231231
}
232232
if (Args.hasArg(options::OPT_fstack_protector) ||
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; Create the symbol 'main'; does not have to be the correct
2+
; signature for 'main', we just need the symbol for the linker
3+
; to fail during the test.
4+
5+
define i32 @main() {
6+
ret i32 0
7+
}

flang/test/Driver/linker-flags.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
! executable and may find the GNU linker from MinGW or Cygwin.
2929
! UNIX-LABEL: "{{.*}}ld{{(\.exe)?}}"
3030
! UNIX-SAME: "[[object_file]]"
31-
! UNIX-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal" "-lm"
31+
! UNIX-SAME: "--whole-archive" "-lFortran_main" "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal" "-lm"
3232

3333
! DARWIN-LABEL: "{{.*}}ld{{(\.exe)?}}"
3434
! DARWIN-SAME: "[[object_file]]"
@@ -38,7 +38,7 @@
3838

3939
! HAIKU-LABEL: "{{.*}}ld{{(\.exe)?}}"
4040
! HAIKU-SAME: "[[object_file]]"
41-
! HAIKU-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal"
41+
! HAIKU-SAME: "--whole-archive" "-lFortran_main" "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal"
4242

4343
! MINGW-LABEL: "{{.*}}ld{{(\.exe)?}}"
4444
! MINGW-SAME: "[[object_file]]"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
! UNSUPPORTED: system-windows
2+
3+
! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/no_duplicate_main.ll
4+
! RUN: %flang -o %t -c %s
5+
! RUN: not %flang -o %t.exe %t %t.c-object 2>&1
6+
7+
! TODO: potentially add further checks to ensure that proper
8+
! linker error messages are detected and checked via
9+
! FileCheck.
10+
11+
program main_dupes
12+
! Irrelevant what to do in here.
13+
! Test is supposed to fail at link time.
14+
end program main_dupes

llvm/docs/AMDGPUUsage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ supported for the ``amdgcn`` target.
820820
that reach other lanes or by explicitly constructing the scratch buffer descriptor,
821821
triggers undefined behavior when it modifies the scratch values of other lanes.
822822
The compiler may assume that such modifications do not occur.
823+
When using code object V5 ``LIBOMPTARGET_STACK_SIZE`` may be used to provide the
824+
private segment size in bytes, for cases where a dynamic stack is used.
823825

824826
**Constant 32-bit**
825827
*TODO*

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Indicate that this is LLVM compiled from the amd-gfx branch. */
1818
#define LLVM_HAVE_BRANCH_AMD_GFX
19-
#define LLVM_MAIN_REVISION 482005
19+
#define LLVM_MAIN_REVISION 482014
2020

2121
/* Define if LLVM_ENABLE_DUMP is enabled */
2222
#cmakedefine LLVM_ENABLE_DUMP

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class TargetMachine {
239239
void setCodeModel(CodeModel::Model CM) { CMModel = CM; }
240240

241241
void setLargeDataThreshold(uint64_t LDT) { LargeDataThreshold = LDT; }
242-
bool isLargeData(const GlobalVariable *GV) const;
242+
bool isLargeGlobalObject(const GlobalObject *GO) const;
243243

244244
bool isPositionIndependent() const;
245245

llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct AddressSanitizerOptions {
2626
bool UseAfterScope = false;
2727
AsanDetectStackUseAfterReturnMode UseAfterReturn =
2828
AsanDetectStackUseAfterReturnMode::Runtime;
29+
int InstrumentationWithCallsThreshold = 7000;
30+
uint32_t MaxInlinePoisoningSize = 64;
31+
bool InsertVersionCheck = true;
2932
};
3033

3134
/// Public interface to the address sanitizer module pass for instrumenting code

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static unsigned getEntrySizeForKind(SectionKind Kind) {
616616
/// DataSections.
617617
static StringRef getSectionPrefixForGlobal(SectionKind Kind, bool IsLarge) {
618618
if (Kind.isText())
619-
return ".text";
619+
return IsLarge ? ".ltext" : ".text";
620620
if (Kind.isReadOnly())
621621
return IsLarge ? ".lrodata" : ".rodata";
622622
if (Kind.isBSS())
@@ -650,10 +650,7 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
650650
Name = ".rodata.cst";
651651
Name += utostr(EntrySize);
652652
} else {
653-
bool IsLarge = false;
654-
if (auto *GV = dyn_cast<GlobalVariable>(GO))
655-
IsLarge = TM.isLargeData(GV);
656-
Name = getSectionPrefixForGlobal(Kind, IsLarge);
653+
Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalObject(GO));
657654
}
658655

659656
bool HasPrefix = false;
@@ -773,12 +770,8 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
773770
Group = C->getName();
774771
IsComdat = C->getSelectionKind() == Comdat::Any;
775772
}
776-
if (auto *GV = dyn_cast<GlobalVariable>(GO)) {
777-
if (TM.isLargeData(GV)) {
778-
assert(TM.getTargetTriple().getArch() == Triple::x86_64);
779-
Flags |= ELF::SHF_X86_64_LARGE;
780-
}
781-
}
773+
if (TM.isLargeGlobalObject(GO))
774+
Flags |= ELF::SHF_X86_64_LARGE;
782775
return {Group, IsComdat, Flags};
783776
}
784777

llvm/lib/Target/TargetMachine.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
3939

4040
TargetMachine::~TargetMachine() = default;
4141

42-
bool TargetMachine::isLargeData(const GlobalVariable *GV) const {
43-
if (getTargetTriple().getArch() != Triple::x86_64 || GV->isThreadLocal())
42+
bool TargetMachine::isLargeGlobalObject(const GlobalObject *GO) const {
43+
if (getTargetTriple().getArch() != Triple::x86_64)
4444
return false;
4545

4646
if (getCodeModel() != CodeModel::Medium && getCodeModel() != CodeModel::Large)
4747
return false;
4848

49+
if (isa<Function>(GO))
50+
return getCodeModel() == CodeModel::Large;
51+
52+
auto *GV = cast<GlobalVariable>(GO);
53+
54+
if (GV->isThreadLocal())
55+
return false;
56+
4957
// Allowing large metadata sections in the presence of an explicit section is
5058
// useful, even if GCC does not allow them. However, we should not mark
5159
// certain well-known prefixes as large, because it would make the whole

llvm/lib/Target/X86/X86Subtarget.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,18 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
8383
if (is64Bit()) {
8484
// 64-bit ELF PIC local references may use GOTOFF relocations.
8585
if (isTargetELF()) {
86-
switch (TM.getCodeModel()) {
87-
// 64-bit small code model is simple: All rip-relative.
88-
case CodeModel::Tiny:
89-
llvm_unreachable("Tiny codesize model not supported on X86");
90-
case CodeModel::Small:
91-
case CodeModel::Kernel:
92-
return X86II::MO_NO_FLAG;
93-
94-
// The large PIC code model uses GOTOFF.
95-
case CodeModel::Large:
96-
return X86II::MO_GOTOFF;
97-
98-
// Medium is a hybrid: RIP-rel for code and non-large data, GOTOFF for
99-
// remaining DSO local data.
100-
case CodeModel::Medium:
101-
// Constant pool and jump table handling pass a nullptr to this
102-
// function so we need to use isa_and_nonnull.
103-
if (isa_and_nonnull<Function>(GV))
104-
return X86II::MO_NO_FLAG; // All code is RIP-relative
105-
if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV)) {
106-
if (TM.isLargeData(GVar))
107-
return X86II::MO_GOTOFF;
108-
}
109-
return X86II::MO_NO_FLAG; // Local symbols use GOTOFF.
110-
}
111-
llvm_unreachable("invalid code model");
86+
CodeModel::Model CM = TM.getCodeModel();
87+
assert(CM != CodeModel::Tiny &&
88+
"Tiny codesize model not supported on X86");
89+
// Large objects use GOTOFF, otherwise use RIP-rel access.
90+
if (auto *GO = dyn_cast_or_null<GlobalObject>(GV))
91+
return TM.isLargeGlobalObject(GO) ? X86II::MO_GOTOFF
92+
: X86II::MO_NO_FLAG;
93+
94+
// For non-GlobalObjects, the small and medium code models treat them as
95+
// accessible with a RIP-rel access. The large code model uses GOTOFF to
96+
// access everything that's not explicitly small.
97+
return CM == CodeModel::Large ? X86II::MO_GOTOFF : X86II::MO_NO_FLAG;
11298
}
11399

114100
// Otherwise, this is either a RIP-relative reference or a 64-bit movabsq,

0 commit comments

Comments
 (0)