Skip to content

Commit d2619c2

Browse files
committed
[Draft] Use Module to get Triple in getDefaultSubtargetFeatures
1 parent 4f5ad22 commit d2619c2

File tree

7 files changed

+20
-16
lines changed

7 files changed

+20
-16
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: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
582582
}
583583

584584
// TargetMachine factory
585-
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
585+
std::unique_ptr<TargetMachine>
586+
TargetMachineBuilder::create(const Module &M) const {
586587
std::string ErrMsg;
587588
const Target *TheTarget =
588589
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
@@ -592,7 +593,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
592593

593594
// Use MAttr as the default set of features.
594595
SubtargetFeatures Features(MAttr);
595-
Features.getDefaultSubtargetFeatures(TheTriple);
596+
Features.getDefaultSubtargetFeatures(M);
596597
std::string FeatureStr = Features.getString();
597598

598599
std::unique_ptr<TargetMachine> TM(
@@ -920,8 +921,8 @@ void ThinLTOCodeGenerator::optimize(Module &TheModule) {
920921
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
921922

922923
// Optimize now
923-
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
924-
DebugPassManager, nullptr);
924+
optimizeModule(TheModule, *TMBuilder.create(TheModule), OptLevel,
925+
Freestanding, DebugPassManager, nullptr);
925926
}
926927

927928
/// Write out the generated object file, either from CacheEntryPath or from
@@ -997,7 +998,8 @@ void ThinLTOCodeGenerator::run() {
997998
/*IsImporting*/ false);
998999

9991000
// CodeGen
1000-
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
1001+
TargetMachine &TM = *TMBuilder.create(*TheModule);
1002+
auto OutputBuffer = codegenModule(*TheModule, TM);
10011003
if (SavedObjectsDirectoryPath.empty())
10021004
ProducedBinaries[count] = std::move(OutputBuffer);
10031005
else
@@ -1185,13 +1187,13 @@ void ThinLTOCodeGenerator::run() {
11851187
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
11861188

11871189
auto &ImportList = ImportLists[ModuleIdentifier];
1190+
TargetMachine &TM = *TMBuilder.create(*TheModule);
11881191
// Run the main process now, and generates a binary
11891192
auto OutputBuffer = ProcessThinLTOModule(
1190-
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1191-
ExportList, GUIDPreservedSymbols,
1192-
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1193-
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
1194-
DebugPassManager);
1193+
*TheModule, *Index, ModuleMap, TM, ImportList, ExportList,
1194+
GUIDPreservedSymbols, ModuleToDefinedGVSummaries[ModuleIdentifier],
1195+
CacheOptions, DisableCodeGen, SaveTempsDir, Freestanding, OptLevel,
1196+
count, DebugPassManager);
11951197

11961198
// Commit to the cache (if enabled)
11971199
CacheEntry.write(*OutputBuffer);

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)