Skip to content

Commit f9307a0

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 abbcfff commit f9307a0

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 getTargetABIMD(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::getTargetABIMD(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::getTargetABIMD(*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::getTargetABIMD(*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/Internalize.h"
6161
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
6262
#include "llvm/Transforms/ObjCARC.h"
63+
#include "llvm/Transforms/Utils/ModuleUtils.h"
6364
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
6465

6566
#include <numeric>
@@ -578,7 +579,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
578579
}
579580

580581
// TargetMachine factory
581-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
582+
std::unique_ptr<TargetMachine>
583+
TargetMachineBuilder::create(const StringRef TargetABI) const {
582584
std::string ErrMsg;
583585
const Target *TheTarget =
584586
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -588,7 +590,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
588590

589591
// Use MAttr as the default set of features.
590592
SubtargetFeatures Features(MAttr);
591-
Features.getDefaultSubtargetFeatures(TheTriple);
593+
Features.getDefaultSubtargetFeatures(TheTriple, TargetABI);
592594
std::string FeatureStr = Features.getString();
593595

594596
std::unique_ptr<TargetMachine> TM(
@@ -914,10 +916,10 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
914916
*/
915917
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
916918
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
917-
919+
StringRef TargetABI = llvm::getTargetABIMD(TheModule);
918920
// Optimize now
919-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
920-
DebugPassManager, nullptr);
921+
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
922+
Freestanding, DebugPassManager, nullptr);
921923
}
922924

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

997+
StringRef TargetABI = llvm::getTargetABIMD(*TheModule);
995998
// CodeGen
996-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
999+
auto OutputBuffer =
1000+
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
9971001
if (SavedObjectsDirectoryPath.empty())
9981002
ProducedBinaries[count] = std::move(OutputBuffer);
9991003
else
@@ -1181,10 +1185,11 @@ void ThinLTOCodeGenerator::run() {
11811185
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11821186

11831187
auto &ImportList = ImportLists[ModuleIdentifier];
1188+
StringRef TargetABI = llvm::getTargetABIMD(*TheModule);
11841189
// Run the main process now, and generates a binary
11851190
auto OutputBuffer = ProcessThinLTOModule(
1186-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1187-
ExportList, GUIDPreservedSymbols,
1191+
*TheModule, *Index, ModuleMap, *TMBuilder.create(TargetABI),
1192+
ImportList, ExportList, GUIDPreservedSymbols,
11881193
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
11891194
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
11901195
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::getTargetABIMD(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)