Skip to content

Commit

Permalink
[Draft] Use Module to get Triple in getDefaultSubtargetFeatures
Browse files Browse the repository at this point in the history
  • Loading branch information
hiraditya committed Jul 27, 2024
1 parent 4f5ad22 commit d2619c2
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct TargetMachineBuilder {
std::optional<Reloc::Model> RelocModel;
CodeGenOptLevel CGOptLevel = CodeGenOptLevel::Aggressive;

std::unique_ptr<TargetMachine> create() const;
std::unique_ptr<TargetMachine> create(const Module &M) const;
};

/// This class define an interface similar to the LTOCodeGenerator, but adapted
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/TargetParser/SubtargetFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/MathExtras.h"
#include <array>
#include <initializer_list>
Expand Down Expand Up @@ -195,7 +196,7 @@ class SubtargetFeatures {
void dump() const;

/// Adds the default features for the specified target triple.
void getDefaultSubtargetFeatures(const Triple& Triple);
void getDefaultSubtargetFeatures(const Module &M);

/// Determine if a feature has a flag; '+' or '-'
static bool hasFlag(StringRef Feature) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static std::unique_ptr<TargetMachine>
createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
StringRef TheTriple = M.getTargetTriple();
SubtargetFeatures Features;
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
Features.getDefaultSubtargetFeatures(M);
for (const std::string &A : Conf.MAttrs)
Features.AddFeature(A);

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ bool LTOCodeGenerator::determineTarget() {
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
// the default set of features.
SubtargetFeatures Features(join(Config.MAttrs, ""));
Features.getDefaultSubtargetFeatures(Triple);
Features.getDefaultSubtargetFeatures(*MergedModule);
FeatureStr = Features.getString();
if (Config.CPU.empty())
Config.CPU = lto::getThinLTODefaultCPU(Triple);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/LTO/LTOModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,

// construct LTOModule, hand over ownership of module and target
SubtargetFeatures Features;
Features.getDefaultSubtargetFeatures(Triple);
Features.getDefaultSubtargetFeatures(*M.get());
std::string FeatureStr = Features.getString();
// Set a default CPU for Darwin triples.
std::string CPU;
Expand Down
22 changes: 12 additions & 10 deletions llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
}

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

// Use MAttr as the default set of features.
SubtargetFeatures Features(MAttr);
Features.getDefaultSubtargetFeatures(TheTriple);
Features.getDefaultSubtargetFeatures(M);
std::string FeatureStr = Features.getString();

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

// Optimize now
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
DebugPassManager, nullptr);
optimizeModule(TheModule, *TMBuilder.create(TheModule), OptLevel,
Freestanding, DebugPassManager, nullptr);
}

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

// CodeGen
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
TargetMachine &TM = *TMBuilder.create(*TheModule);
auto OutputBuffer = codegenModule(*TheModule, TM);
if (SavedObjectsDirectoryPath.empty())
ProducedBinaries[count] = std::move(OutputBuffer);
else
Expand Down Expand Up @@ -1185,13 +1187,13 @@ void ThinLTOCodeGenerator::run() {
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");

auto &ImportList = ImportLists[ModuleIdentifier];
TargetMachine &TM = *TMBuilder.create(*TheModule);
// Run the main process now, and generates a binary
auto OutputBuffer = ProcessThinLTOModule(
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
ExportList, GUIDPreservedSymbols,
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
DebugPassManager);
*TheModule, *Index, ModuleMap, TM, ImportList, ExportList,
GUIDPreservedSymbols, ModuleToDefinedGVSummaries[ModuleIdentifier],
CacheOptions, DisableCodeGen, SaveTempsDir, Freestanding, OptLevel,
count, DebugPassManager);

// Commit to the cache (if enabled)
CacheEntry.write(*OutputBuffer);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/TargetParser/SubtargetFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
}
#endif

void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
void SubtargetFeatures::getDefaultSubtargetFeatures(const Module &M) {
llvm::Triple Triple(M.getTargetTriple());
// FIXME: This is an inelegant way of specifying the features of a
// subtarget. It would be better if we could encode this information
// into the IR.
Expand Down

0 comments on commit d2619c2

Please sign in to comment.