Skip to content

Commit 06d0e15

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: llvm#69780) Having a target-abi will provide a way to add minimal requirements for target-features like `+d` for RISC-V.
1 parent d4ddf06 commit 06d0e15

File tree

9 files changed

+38
-14
lines changed

9 files changed

+38
-14
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/include/llvm/Transforms/Utils/ModuleUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
9797
/// Adds global values to the llvm.compiler.used list.
9898
void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
9999

100+
/// Returns the TargetABI Metadata if present, empty StringRef otherwise.
101+
StringRef getTargetABIFromMD(Module &M);
102+
100103
/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
101104
/// ShouldRemove should return true for any initializer field that should not be
102105
/// included in the replacement global.

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 = llvm::getTargetABIFromMD(M);
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+
StringRef TargetABI = llvm::getTargetABIFromMD(*MergedModule);
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 = llvm::getTargetABIFromMD(*M);
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: 13 additions & 8 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(
@@ -913,10 +915,10 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
913915
*/
914916
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
915917
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
916-
918+
StringRef TargetABI = llvm::getTargetABIFromMD(TheModule);
917919
// Optimize now
918-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
919-
DebugPassManager, nullptr);
920+
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
921+
Freestanding, DebugPassManager, nullptr);
920922
}
921923

922924
/// Write out the generated object file, either from CacheEntryPath or from
@@ -991,8 +993,10 @@ void ThinLTOCodeGenerator::run() {
991993
auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
992994
/*IsImporting*/ false);
993995

996+
StringRef TargetABI = llvm::getTargetABIFromMD(*TheModule);
994997
// CodeGen
995-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
998+
auto OutputBuffer =
999+
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
9961000
if (SavedObjectsDirectoryPath.empty())
9971001
ProducedBinaries[count] = std::move(OutputBuffer);
9981002
else
@@ -1177,10 +1181,11 @@ void ThinLTOCodeGenerator::run() {
11771181
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11781182

11791183
auto &ImportList = ImportLists[ModuleIdentifier];
1184+
StringRef TargetABI = llvm::getTargetABIFromMD(*TheModule);
11801185
// Run the main process now, and generates a binary
11811186
auto OutputBuffer = ProcessThinLTOModule(
1182-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1183-
ExportList, GUIDPreservedSymbols,
1187+
*TheModule, *Index, ModuleMap, *TMBuilder.create(TargetABI),
1188+
ImportList, ExportList, GUIDPreservedSymbols,
11841189
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
11851190
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
11861191
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.isRISCV64() && TargetABI.contains("lp64d")) {
86+
AddFeature("+d");
8487
}
8588
}

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
163163
appendToUsedList(M, "llvm.compiler.used", Values);
164164
}
165165

166+
StringRef llvm::getTargetABIFromMD(Module &M) {
167+
StringRef TargetABI = "";
168+
if (auto *TargetABIMD =
169+
dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi")))
170+
TargetABI = TargetABIMD->getString();
171+
return TargetABI;
172+
}
173+
166174
static void removeFromUsedList(Module &M, StringRef Name,
167175
function_ref<bool(Constant *)> ShouldRemove) {
168176
GlobalVariable *GV = M.getNamedGlobal(Name);

0 commit comments

Comments
 (0)