Skip to content

Commit a882c96

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 0ab3d6e commit a882c96

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
@@ -402,7 +402,12 @@ bool LTOCodeGenerator::determineTarget() {
402402
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
403403
// the default set of features.
404404
SubtargetFeatures Features(join(Config.MAttrs, ""));
405-
Features.getDefaultSubtargetFeatures(Triple);
405+
StringRef TargetABI = "";
406+
if (auto TargetABIMD =
407+
dyn_cast_or_null<MDString>(MergedModule->getModuleFlag("target-abi")))
408+
TargetABI = TargetABIMD->getString();
409+
410+
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
406411
FeatureStr = Features.getString();
407412
if (Config.CPU.empty())
408413
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
@@ -578,7 +578,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
578578
}
579579

580580
// TargetMachine factory
581-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
581+
std::unique_ptr<TargetMachine>
582+
TargetMachineBuilder::create(const StringRef TargetABI) const {
582583
std::string ErrMsg;
583584
const Target *TheTarget =
584585
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -588,7 +589,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
588589

589590
// Use MAttr as the default set of features.
590591
SubtargetFeatures Features(MAttr);
591-
Features.getDefaultSubtargetFeatures(TheTriple);
592+
Features.getDefaultSubtargetFeatures(TheTriple, TargetABI);
592593
std::string FeatureStr = Features.getString();
593594

594595
std::unique_ptr<TargetMachine> TM(
@@ -914,10 +915,13 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
914915
*/
915916
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
916917
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
917-
918+
StringRef TargetABI = "";
919+
if (auto TargetABIMD =
920+
dyn_cast_or_null<MDString>(TheModule.getModuleFlag("target-abi")))
921+
TargetABI = TargetABIMD->getString();
918922
// Optimize now
919-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
920-
DebugPassManager, nullptr);
923+
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
924+
Freestanding, DebugPassManager, nullptr);
921925
}
922926

923927
/// Write out the generated object file, either from CacheEntryPath or from
@@ -992,8 +996,13 @@ void ThinLTOCodeGenerator::run() {
992996
auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
993997
/*IsImporting*/ false);
994998

999+
StringRef TargetABI = "";
1000+
if (auto TargetABIMD = dyn_cast_or_null<MDString>(
1001+
TheModule->getModuleFlag("target-abi")))
1002+
TargetABI = TargetABIMD->getString();
9951003
// CodeGen
996-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
1004+
auto OutputBuffer =
1005+
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
9971006
if (SavedObjectsDirectoryPath.empty())
9981007
ProducedBinaries[count] = std::move(OutputBuffer);
9991008
else
@@ -1181,10 +1190,14 @@ void ThinLTOCodeGenerator::run() {
11811190
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11821191

11831192
auto &ImportList = ImportLists[ModuleIdentifier];
1193+
StringRef TargetABI = "";
1194+
if (auto TargetABIMD = dyn_cast_or_null<MDString>(
1195+
TheModule->getModuleFlag("target-abi")))
1196+
TargetABI = TargetABIMD->getString();
11841197
// Run the main process now, and generates a binary
11851198
auto OutputBuffer = ProcessThinLTOModule(
1186-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1187-
ExportList, GUIDPreservedSymbols,
1199+
*TheModule, *Index, ModuleMap, *TMBuilder.create(TargetABI),
1200+
ImportList, ExportList, GUIDPreservedSymbols,
11881201
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
11891202
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
11901203
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)