Skip to content

Commit 6052288

Browse files
committed
[Draft] Use Module to get Triple in getDefaultSubtargetFeatures
Module can be used to query target-abi (follow up patch) which can be used to populate default subtarget features. 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 4f5ad22 commit 6052288

File tree

7 files changed

+15
-11
lines changed

7 files changed

+15
-11
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 Module &M) 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
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/ArrayRef.h"
2121
#include "llvm/ADT/STLExtras.h"
2222
#include "llvm/ADT/StringRef.h"
23+
#include "llvm/IR/Module.h"
2324
#include "llvm/Support/MathExtras.h"
2425
#include <array>
2526
#include <initializer_list>
@@ -195,7 +196,7 @@ class SubtargetFeatures {
195196
void dump() const;
196197

197198
/// Adds the default features for the specified target triple.
198-
void getDefaultSubtargetFeatures(const Triple& Triple);
199+
void getDefaultSubtargetFeatures(const Module &M);
199200

200201
/// Determine if a feature has a flag; '+' or '-'
201202
static bool hasFlag(StringRef Feature) {

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ 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+
Features.getDefaultSubtargetFeatures(M);
205205
for (const std::string &A : Conf.MAttrs)
206206
Features.AddFeature(A);
207207

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ bool LTOCodeGenerator::determineTarget() {
406406
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
407407
// the default set of features.
408408
SubtargetFeatures Features(join(Config.MAttrs, ""));
409-
Features.getDefaultSubtargetFeatures(Triple);
409+
Features.getDefaultSubtargetFeatures(*MergedModule);
410410
FeatureStr = Features.getString();
411411
if (Config.CPU.empty())
412412
Config.CPU = lto::getThinLTODefaultCPU(Triple);

llvm/lib/LTO/LTOModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
213213

214214
// construct LTOModule, hand over ownership of module and target
215215
SubtargetFeatures Features;
216-
Features.getDefaultSubtargetFeatures(Triple);
216+
Features.getDefaultSubtargetFeatures(*M.get());
217217
std::string FeatureStr = Features.getString();
218218
// Set a default CPU for Darwin triples.
219219
std::string CPU;

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
582582
}
583583

584584
// TargetMachine factory
585-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
585+
std::unique_ptr<TargetMachine> TargetMachineBuilder::create(const Module &M) const {
586586
std::string ErrMsg;
587587
const Target *TheTarget =
588588
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -592,7 +592,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
592592

593593
// Use MAttr as the default set of features.
594594
SubtargetFeatures Features(MAttr);
595-
Features.getDefaultSubtargetFeatures(TheTriple);
595+
Features.getDefaultSubtargetFeatures(M);
596596
std::string FeatureStr = Features.getString();
597597

598598
std::unique_ptr<TargetMachine> TM(
@@ -920,7 +920,7 @@ void ThinLTOCodeGenerator::optimize(Module &TheModule) {
920920
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
921921

922922
// Optimize now
923-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
923+
optimizeModule(TheModule, *TMBuilder.create(TheModule), OptLevel, Freestanding,
924924
DebugPassManager, nullptr);
925925
}
926926

@@ -997,7 +997,8 @@ void ThinLTOCodeGenerator::run() {
997997
/*IsImporting*/ false);
998998

999999
// CodeGen
1000-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
1000+
TargetMachine &TM = *TMBuilder.create(*TheModule);
1001+
auto OutputBuffer = codegenModule(*TheModule, TM);
10011002
if (SavedObjectsDirectoryPath.empty())
10021003
ProducedBinaries[count] = std::move(OutputBuffer);
10031004
else
@@ -1185,9 +1186,10 @@ void ThinLTOCodeGenerator::run() {
11851186
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11861187

11871188
auto &ImportList = ImportLists[ModuleIdentifier];
1189+
TargetMachine &TM = *TMBuilder.create(*TheModule);
11881190
// Run the main process now, and generates a binary
11891191
auto OutputBuffer = ProcessThinLTOModule(
1190-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1192+
*TheModule, *Index, ModuleMap, TM, ImportList,
11911193
ExportList, GUIDPreservedSymbols,
11921194
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
11931195
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,

llvm/lib/TargetParser/SubtargetFeature.cpp

Lines changed: 2 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 Module &M) {
72+
llvm::Triple Triple(M.getTargetTriple());
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.

0 commit comments

Comments
 (0)