Skip to content

Commit 04a4523

Browse files
committed
Use TargetABI to assign default-target features in getDefaultSubtargetFeatures
It is currently not possible to provide any reasonable target-features for compiler generated functions (See: #69780) Having a target-abi will provide a way to add minimal requirements for target-features like `+d` for RISC-V.
1 parent 6aa723d commit 04a4523

File tree

7 files changed

+45
-15
lines changed

7 files changed

+45
-15
lines changed

llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct TargetMachineBuilder {
4040
std::optional<Reloc::Model> RelocModel;
4141
CodeGenOptLevel CGOptLevel = CodeGenOptLevel::Aggressive;
4242

43-
std::unique_ptr<TargetMachine> create() const;
43+
std::unique_ptr<TargetMachine> create(const StringRef TargetABI) const;
4444
};
4545

4646
/// This class define an interface similar to the LTOCodeGenerator, but adapted

llvm/include/llvm/TargetParser/SubtargetFeature.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ class SubtargetFeatures {
195195
void dump() const;
196196

197197
/// Adds the default features for the specified target triple.
198-
void getDefaultSubtargetFeatures(const Triple& Triple);
198+
void getDefaultSubtargetFeatures(const Triple &Triple,
199+
const StringRef ABIInfo);
199200

200201
/// Determine if a feature has a flag; '+' or '-'
201202
static bool hasFlag(StringRef Feature) {

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ static std::unique_ptr<TargetMachine>
201201
createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
202202
StringRef TheTriple = M.getTargetTriple();
203203
SubtargetFeatures Features;
204-
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
204+
StringRef TargetABI = "";
205+
if (auto TargetABIMD =
206+
dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi")))
207+
TargetABI = TargetABIMD->getString();
208+
209+
Features.getDefaultSubtargetFeatures(Triple(TheTriple), TargetABI);
205210
for (const std::string &A : Conf.MAttrs)
206211
Features.AddFeature(A);
207212

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,12 @@ bool LTOCodeGenerator::determineTarget() {
406406
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
407407
// the default set of features.
408408
SubtargetFeatures Features(join(Config.MAttrs, ""));
409-
Features.getDefaultSubtargetFeatures(Triple);
409+
StringRef TargetABI = "";
410+
if (auto TargetABIMD =
411+
dyn_cast_or_null<MDString>(MergedModule->getModuleFlag("target-abi")))
412+
TargetABI = TargetABIMD->getString();
413+
414+
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
410415
FeatureStr = Features.getString();
411416
if (Config.CPU.empty())
412417
Config.CPU = lto::getThinLTODefaultCPU(Triple);

llvm/lib/LTO/LTOModule.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
204204
if (TripleStr.empty())
205205
TripleStr = sys::getDefaultTargetTriple();
206206
llvm::Triple Triple(TripleStr);
207-
207+
StringRef TargetABI = "";
208+
if (auto TargetABIMD =
209+
dyn_cast_or_null<MDString>(M->getModuleFlag("target-abi")))
210+
TargetABI = TargetABIMD->getString();
208211
// find machine architecture for this module
209212
std::string errMsg;
210213
const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
@@ -213,7 +216,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
213216

214217
// construct LTOModule, hand over ownership of module and target
215218
SubtargetFeatures Features;
216-
Features.getDefaultSubtargetFeatures(Triple);
219+
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
217220
std::string FeatureStr = Features.getString();
218221
// Set a default CPU for Darwin triples.
219222
std::string CPU;

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
582582
}
583583

584584
// TargetMachine factory
585-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
585+
std::unique_ptr<TargetMachine>
586+
TargetMachineBuilder::create(const StringRef TargetABI) const {
586587
std::string ErrMsg;
587588
const Target *TheTarget =
588589
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -592,7 +593,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
592593

593594
// Use MAttr as the default set of features.
594595
SubtargetFeatures Features(MAttr);
595-
Features.getDefaultSubtargetFeatures(TheTriple);
596+
Features.getDefaultSubtargetFeatures(TheTriple, TargetABI);
596597
std::string FeatureStr = Features.getString();
597598

598599
std::unique_ptr<TargetMachine> TM(
@@ -918,10 +919,13 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
918919
*/
919920
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
920921
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
921-
922+
StringRef TargetABI = "";
923+
if (auto TargetABIMD =
924+
dyn_cast_or_null<MDString>(TheModule.getModuleFlag("target-abi")))
925+
TargetABI = TargetABIMD->getString();
922926
// Optimize now
923-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
924-
DebugPassManager, nullptr);
927+
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
928+
Freestanding, DebugPassManager, nullptr);
925929
}
926930

927931
/// Write out the generated object file, either from CacheEntryPath or from
@@ -996,8 +1000,13 @@ void ThinLTOCodeGenerator::run() {
9961000
auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
9971001
/*IsImporting*/ false);
9981002

1003+
StringRef TargetABI = "";
1004+
if (auto TargetABIMD = dyn_cast_or_null<MDString>(
1005+
TheModule->getModuleFlag("target-abi")))
1006+
TargetABI = TargetABIMD->getString();
9991007
// CodeGen
1000-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
1008+
auto OutputBuffer =
1009+
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
10011010
if (SavedObjectsDirectoryPath.empty())
10021011
ProducedBinaries[count] = std::move(OutputBuffer);
10031012
else
@@ -1185,10 +1194,14 @@ void ThinLTOCodeGenerator::run() {
11851194
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11861195

11871196
auto &ImportList = ImportLists[ModuleIdentifier];
1197+
StringRef TargetABI = "";
1198+
if (auto TargetABIMD = dyn_cast_or_null<MDString>(
1199+
TheModule->getModuleFlag("target-abi")))
1200+
TargetABI = TargetABIMD->getString();
11881201
// Run the main process now, and generates a binary
11891202
auto OutputBuffer = ProcessThinLTOModule(
1190-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1191-
ExportList, GUIDPreservedSymbols,
1203+
*TheModule, *Index, ModuleMap, *TMBuilder.create(TargetABI),
1204+
ImportList, ExportList, GUIDPreservedSymbols,
11921205
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
11931206
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
11941207
DebugPassManager);

llvm/lib/TargetParser/SubtargetFeature.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
6868
}
6969
#endif
7070

71-
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
71+
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple &Triple,
72+
const StringRef TargetABI) {
7273
// FIXME: This is an inelegant way of specifying the features of a
7374
// subtarget. It would be better if we could encode this information
7475
// into the IR.
@@ -81,5 +82,7 @@ void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
8182
AddFeature("64bit");
8283
AddFeature("altivec");
8384
}
85+
} else if (Triple.isAndroid() && TargetABI.contains("lp64d")) {
86+
AddFeature("+d");
8487
}
8588
}

0 commit comments

Comments
 (0)