Skip to content

Commit e11196d

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 88c18da commit e11196d

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
@@ -47,7 +47,7 @@ struct ThinLTOCodeGeneratorImpl::TargetMachineBuilder {
4747
std::optional<Reloc::Model> RelocModel;
4848
CodeGenOptLevel CGOptLevel = CodeGenOptLevel::Aggressive;
4949

50-
std::unique_ptr<TargetMachine> create() const;
50+
std::unique_ptr<TargetMachine> create(const StringRef TargetABI) const;
5151
};
5252

5353
/// 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
@@ -204,7 +204,9 @@ static std::unique_ptr<TargetMachine>
204204
createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
205205
StringRef TheTriple = M.getTargetTriple();
206206
SubtargetFeatures Features;
207-
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
207+
StringRef TargetABI = Conf.Options.MCOptions.getABIName();
208+
209+
Features.getDefaultSubtargetFeatures(Triple(TheTriple), TargetABI);
208210
for (const std::string &A : Conf.MAttrs)
209211
Features.AddFeature(A);
210212

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

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

578579
// TargetMachine factory
579-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
580+
std::unique_ptr<TargetMachine>
581+
TargetMachineBuilder::create(const StringRef TargetABI) const {
580582
std::string ErrMsg;
581583
const Target *TheTarget =
582584
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -586,7 +588,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
586588

587589
// Use MAttr as the default set of features.
588590
SubtargetFeatures Features(MAttr);
589-
Features.getDefaultSubtargetFeatures(TheTriple);
591+
Features.getDefaultSubtargetFeatures(TheTriple, TargetABI);
590592
std::string FeatureStr = Features.getString();
591593

592594
std::unique_ptr<TargetMachine> TM(
@@ -911,10 +913,11 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
911913
*/
912914
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
913915
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
916+
StringRef TargetABI = TMBuilder.Options.MCOptions.getABIName();
914917

915918
// Optimize now
916-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
917-
DebugPassManager, nullptr);
919+
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
920+
Freestanding, DebugPassManager, nullptr);
918921
}
919922

920923
/// Write out the generated object file, either from CacheEntryPath or from
@@ -988,9 +991,11 @@ void ThinLTOCodeGenerator::run() {
988991
// Parse module now
989992
auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
990993
/*IsImporting*/ false);
994+
StringRef TargetABI = TMBuilder.Options.MCOptions.getABIName();
991995

992996
// CodeGen
993-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
997+
auto OutputBuffer =
998+
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
994999
if (SavedObjectsDirectoryPath.empty())
9951000
ProducedBinaries[count] = std::move(OutputBuffer);
9961001
else
@@ -1175,10 +1180,12 @@ void ThinLTOCodeGenerator::run() {
11751180
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11761181

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

llvm/lib/TargetParser/SubtargetFeature.cpp

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

70-
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
70+
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple &Triple,
71+
const StringRef TargetABI) {
7172
// FIXME: This is an inelegant way of specifying the features of a
7273
// subtarget. It would be better if we could encode this information
7374
// into the IR.
@@ -80,5 +81,14 @@ void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
8081
AddFeature("64bit");
8182
AddFeature("altivec");
8283
}
84+
} else if (Triple.isRISCV64()) {
85+
if (TargetABI.contains("lp64d"))
86+
AddFeature("+d");
87+
if (TargetABI.contains("lp64f"))
88+
AddFeature("+f");
89+
if (TargetABI.contains("lp64q"))
90+
AddFeature("+q");
91+
} else if (Triple.isRISCV32() && TargetABI.contains("ilp32f")) {
92+
AddFeature("+f");
8393
}
8494
}

0 commit comments

Comments
 (0)