Skip to content

Commit

Permalink
Use TargetABI to assign default-target features in getDefaultSubtarge…
Browse files Browse the repository at this point in the history
…tFeatures

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.
  • Loading branch information
hiraditya committed Sep 6, 2024
1 parent d4ddf06 commit 06d0e15
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 14 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 StringRef TargetABI) 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 @@ -195,7 +195,8 @@ class SubtargetFeatures {
void dump() const;

/// Adds the default features for the specified target triple.
void getDefaultSubtargetFeatures(const Triple& Triple);
void getDefaultSubtargetFeatures(const Triple &Triple,
const StringRef ABIInfo);

/// Determine if a feature has a flag; '+' or '-'
static bool hasFlag(StringRef Feature) {
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Transforms/Utils/ModuleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
/// Adds global values to the llvm.compiler.used list.
void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);

/// Returns the TargetABI Metadata if present, empty StringRef otherwise.
StringRef getTargetABIFromMD(Module &M);

/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
/// ShouldRemove should return true for any initializer field that should not be
/// included in the replacement global.
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ static std::unique_ptr<TargetMachine>
createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
StringRef TheTriple = M.getTargetTriple();
SubtargetFeatures Features;
Features.getDefaultSubtargetFeatures(Triple(TheTriple));
StringRef TargetABI = llvm::getTargetABIFromMD(M);

Features.getDefaultSubtargetFeatures(Triple(TheTriple), TargetABI);
for (const std::string &A : Conf.MAttrs)
Features.AddFeature(A);

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ 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);
StringRef TargetABI = llvm::getTargetABIFromMD(*MergedModule);
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
FeatureStr = Features.getString();
if (Config.CPU.empty())
Config.CPU = lto::getThinLTODefaultCPU(Triple);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/LTOModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
if (TripleStr.empty())
TripleStr = sys::getDefaultTargetTriple();
llvm::Triple Triple(TripleStr);
StringRef TargetABI = llvm::getTargetABIFromMD(*M);

// find machine architecture for this module
std::string errMsg;
Expand All @@ -213,7 +214,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,

// construct LTOModule, hand over ownership of module and target
SubtargetFeatures Features;
Features.getDefaultSubtargetFeatures(Triple);
Features.getDefaultSubtargetFeatures(Triple, TargetABI);
std::string FeatureStr = Features.getString();
// Set a default CPU for Darwin triples.
std::string CPU;
Expand Down
21 changes: 13 additions & 8 deletions llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
#include "llvm/Transforms/ObjCARC.h"
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

#include <numeric>

Expand Down Expand Up @@ -577,7 +578,8 @@ void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
}

// TargetMachine factory
std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
std::unique_ptr<TargetMachine>
TargetMachineBuilder::create(const StringRef TargetABI) const {
std::string ErrMsg;
const Target *TheTarget =
TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
Expand All @@ -587,7 +589,7 @@ std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {

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

std::unique_ptr<TargetMachine> TM(
Expand Down Expand Up @@ -913,10 +915,10 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
*/
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));

StringRef TargetABI = llvm::getTargetABIFromMD(TheModule);
// Optimize now
optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
DebugPassManager, nullptr);
optimizeModule(TheModule, *TMBuilder.create(TargetABI), OptLevel,
Freestanding, DebugPassManager, nullptr);
}

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

StringRef TargetABI = llvm::getTargetABIFromMD(*TheModule);
// CodeGen
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
auto OutputBuffer =
codegenModule(*TheModule, *TMBuilder.create(TargetABI));
if (SavedObjectsDirectoryPath.empty())
ProducedBinaries[count] = std::move(OutputBuffer);
else
Expand Down Expand Up @@ -1177,10 +1181,11 @@ void ThinLTOCodeGenerator::run() {
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");

auto &ImportList = ImportLists[ModuleIdentifier];
StringRef TargetABI = llvm::getTargetABIFromMD(*TheModule);
// Run the main process now, and generates a binary
auto OutputBuffer = ProcessThinLTOModule(
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
ExportList, GUIDPreservedSymbols,
*TheModule, *Index, ModuleMap, *TMBuilder.create(TargetABI),
ImportList, ExportList, GUIDPreservedSymbols,
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
DebugPassManager);
Expand Down
5 changes: 4 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 Triple &Triple,
const StringRef TargetABI) {
// 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 All @@ -81,5 +82,7 @@ void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
AddFeature("64bit");
AddFeature("altivec");
}
} else if (Triple.isRISCV64() && TargetABI.contains("lp64d")) {
AddFeature("+d");
}
}
8 changes: 8 additions & 0 deletions llvm/lib/Transforms/Utils/ModuleUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
appendToUsedList(M, "llvm.compiler.used", Values);
}

StringRef llvm::getTargetABIFromMD(Module &M) {
StringRef TargetABI = "";
if (auto *TargetABIMD =
dyn_cast_or_null<MDString>(M.getModuleFlag("target-abi")))
TargetABI = TargetABIMD->getString();
return TargetABI;
}

static void removeFromUsedList(Module &M, StringRef Name,
function_ref<bool(Constant *)> ShouldRemove) {
GlobalVariable *GV = M.getNamedGlobal(Name);
Expand Down

0 comments on commit 06d0e15

Please sign in to comment.