Skip to content

Commit be73339

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 aabddc9 commit be73339

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ 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 = Conf.Options.MCOptions.getABIName();
205+
206+
Features.getDefaultSubtargetFeatures(Triple(TheTriple), TargetABI);
205207
for (const std::string &A : Conf.MAttrs)
206208
Features.AddFeature(A);
207209

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ 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+
auto TargetABI = Config.Options.MCOptions.getABIName();
406+
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
406407
FeatureStr = Features.getString();
407408
if (Config.CPU.empty())
408409
Config.CPU = lto::getThinLTODefaultCPU(Triple);

llvm/lib/LTO/LTOModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
204204
if (TripleStr.empty())
205205
TripleStr = sys::getDefaultTargetTriple();
206206
llvm::Triple Triple(TripleStr);
207+
StringRef TargetABI = options.MCOptions.getABIName();
207208

208209
// find machine architecture for this module
209210
std::string errMsg;
@@ -213,7 +214,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
213214

214215
// construct LTOModule, hand over ownership of module and target
215216
SubtargetFeatures Features;
216-
Features.getDefaultSubtargetFeatures(Triple);
217+
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
217218
std::string FeatureStr = Features.getString();
218219
// Set a default CPU for Darwin triples.
219220
std::string CPU;

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
6161
#include "llvm/Transforms/ObjCARC.h"
6262
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
63+
#include "llvm/Transforms/Utils/ModuleUtils.h"
6364

6465
#include <numeric>
6566

@@ -577,7 +578,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
577578
}
578579

579580
// TargetMachine factory
580-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
581+
std::unique_ptr<TargetMachine>
582+
TargetMachineBuilder::create(const StringRef TargetABI) const {
581583
std::string ErrMsg;
582584
const Target *TheTarget =
583585
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -587,7 +589,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
587589

588590
// Use MAttr as the default set of features.
589591
SubtargetFeatures Features(MAttr);
590-
Features.getDefaultSubtargetFeatures(TheTriple);
592+
Features.getDefaultSubtargetFeatures(TheTriple, TargetABI);
591593
std::string FeatureStr = Features.getString();
592594

593595
std::unique_ptr<TargetMachine> TM(
@@ -912,10 +914,11 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
912914
*/
913915
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
914916
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
917+
StringRef TargetABI = TMBuilder.Options.MCOptions.getABIName();
915918

916919
// Optimize now
917-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
918-
DebugPassManager, nullptr);
920+
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
921+
Freestanding, DebugPassManager, nullptr);
919922
}
920923

921924
/// Write out the generated object file, either from CacheEntryPath or from
@@ -989,9 +992,11 @@ void ThinLTOCodeGenerator::run() {
989992
// Parse module now
990993
auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
991994
/*IsImporting*/ false);
995+
StringRef TargetABI = TMBuilder.Options.MCOptions.getABIName();
992996

993997
// CodeGen
994-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
998+
auto OutputBuffer =
999+
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
9951000
if (SavedObjectsDirectoryPath.empty())
9961001
ProducedBinaries[count] = std::move(OutputBuffer);
9971002
else
@@ -1176,10 +1181,12 @@ void ThinLTOCodeGenerator::run() {
11761181
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11771182

11781183
auto &ImportList = ImportLists[ModuleIdentifier];
1184+
StringRef TargetABI = TMBuilder.Options.MCOptions.getABIName();
1185+
11791186
// Run the main process now, and generates a binary
11801187
auto OutputBuffer = ProcessThinLTOModule(
1181-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1182-
ExportList, GUIDPreservedSymbols,
1188+
*TheModule, *Index, ModuleMap, *TMBuilder.create(TargetABI),
1189+
ImportList, ExportList, GUIDPreservedSymbols,
11831190
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
11841191
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
11851192
DebugPassManager);

llvm/lib/TargetParser/SubtargetFeature.cpp

Lines changed: 11 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,14 @@ void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
8182
AddFeature("64bit");
8283
AddFeature("altivec");
8384
}
85+
} else if (Triple.isRISCV64()) {
86+
if (TargetABI.contains("lp64d"))
87+
AddFeature("+d");
88+
if (TargetABI.contains("lp64f"))
89+
AddFeature("+f");
90+
if (TargetABI.contains("lp64q"))
91+
AddFeature("+q");
92+
} else if (Triple.isRISCV32() && TargetABI.contains("ilp32f")) {
93+
AddFeature("+f");
8494
}
8595
}

0 commit comments

Comments
 (0)