Skip to content

[IR] Store Triple in Module (NFC) #129868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
// Create the TargetMachine for generating code.
std::string Error;
std::string Triple = TheModule->getTargetTriple();
std::string Triple = TheModule->getTargetTriple().str();
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
if (!TheTarget) {
if (MustCreateTM)
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ CodeGenAction::loadModule(MemoryBufferRef MBRef) {
// linker using merged object file.
if (!Bm) {
auto M = std::make_unique<llvm::Module>("empty", *VMContext);
M->setTargetTriple(CI.getTargetOpts().Triple);
M->setTargetTriple(Triple(CI.getTargetOpts().Triple));
return M;
}
Expected<std::unique_ptr<llvm::Module>> MOrErr =
Expand Down Expand Up @@ -1123,10 +1123,10 @@ void CodeGenAction::ExecuteAction() {
return;

const TargetOptions &TargetOpts = CI.getTargetOpts();
if (TheModule->getTargetTriple() != TargetOpts.Triple) {
if (TheModule->getTargetTriple().str() != TargetOpts.Triple) {
Diagnostics.Report(SourceLocation(), diag::warn_fe_override_module)
<< TargetOpts.Triple;
TheModule->setTargetTriple(TargetOpts.Triple);
TheModule->setTargetTriple(Triple(TargetOpts.Triple));
}

EmbedObject(TheModule.get(), CodeGenOpts, Diagnostics);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/ModuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace {
void Initialize(ASTContext &Context) override {
Ctx = &Context;

M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
M->setTargetTriple(Ctx->getTargetInfo().getTriple());
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion();
if (!SDKVersion.empty())
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class PCHContainerGenerator : public ASTConsumer {
if (Diags.hasErrorOccurred())
return;

M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
M->setTargetTriple(Ctx.getTargetInfo().getTriple());
M->setDataLayout(Ctx.getTargetInfo().getDataLayoutString());

// PCH files don't have a signature field in the control block,
Expand All @@ -274,7 +274,7 @@ class PCHContainerGenerator : public ASTConsumer {
// Ensure the target exists.
std::string Error;
auto Triple = Ctx.getTargetInfo().getTriple();
if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
if (!llvm::TargetRegistry::lookupTarget(Triple, Error))
llvm::report_fatal_error(llvm::Twine(Error));

// Emit the serialized Clang AST into its own section.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Interpreter/DeviceOffload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() {
std::error_code());
llvm::TargetOptions TO = llvm::TargetOptions();
llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
PTU.TheModule->getTargetTriple(), TargetOpts.CPU, "", TO,
PTU.TheModule->getTargetTriple().str(), TargetOpts.CPU, "", TO,
llvm::Reloc::Model::PIC_);
PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());

Expand Down
4 changes: 2 additions & 2 deletions clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ static std::string OptLLVM(const std::string &IR, CodeGenOptLevel OLvl) {
ErrorAndExit(E);

std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
M->getTargetTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
Options, codegen::getExplicitRelocModel(),
M->getTargetTriple().str(), codegen::getCPUStr(),
codegen::getFeaturesStr(), Options, codegen::getExplicitRelocModel(),
codegen::getExplicitCodeModel(), OLvl));
if (!TM)
ErrorAndExit("Could not create target machine");
Expand Down
10 changes: 5 additions & 5 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,11 @@ Expected<StringRef> compileModule(Module &M, OffloadKind Kind) {
return createStringError(Msg);

auto Options =
codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple()));
codegen::InitTargetOptionsFromCodeGenFlags(M.getTargetTriple());
StringRef CPU = "";
StringRef Features = "";
std::unique_ptr<TargetMachine> TM(
T->createTargetMachine(M.getTargetTriple(), CPU, Features, Options,
T->createTargetMachine(M.getTargetTriple().str(), CPU, Features, Options,
Reloc::PIC_, M.getCodeModel()));

if (M.getDataLayout().isDefault())
Expand All @@ -650,7 +650,7 @@ Expected<StringRef> compileModule(Module &M, OffloadKind Kind) {
auto OS = std::make_unique<llvm::raw_fd_ostream>(FD, true);

legacy::PassManager CodeGenPasses;
TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
TargetLibraryInfoImpl TLII(M.getTargetTriple());
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr,
CodeGenFileType::ObjectFile))
Expand All @@ -674,8 +674,8 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,

LLVMContext Context;
Module M("offload.wrapper.module", Context);
M.setTargetTriple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
M.setTargetTriple(Triple(
Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple())));

switch (Kind) {
case OFK_OpenMP:
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Interpreter/InterpreterExtensionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class InterpreterExtensionsTest : public InterpreterTestBase {
TT.setOS(llvm::Triple::UnknownOS);

std::string UnusedErr;
return llvm::TargetRegistry::lookupTarget(TT.str(), UnusedErr);
return llvm::TargetRegistry::lookupTarget(TT, UnusedErr);
}
};

Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,10 +1389,10 @@ void CodeGenAction::executeAction() {
// given on the command-line).
llvm::TargetMachine &targetMachine = ci.getTargetMachine();

const std::string &theTriple = targetMachine.getTargetTriple().str();
const llvm::Triple &theTriple = targetMachine.getTargetTriple();

if (llvmModule->getTargetTriple() != theTriple) {
diags.Report(clang::diag::warn_fe_override_module) << theTriple;
diags.Report(clang::diag::warn_fe_override_module) << theTriple.str();
}

// Always set the triple and data layout, to make sure they match and are set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ EmulateInstructionMIPS::EmulateInstructionMIPS(
std::string Status;
llvm::Triple triple = arch.GetTriple();
const llvm::Target *target =
llvm::TargetRegistry::lookupTarget(triple.getTriple(), Status);
llvm::TargetRegistry::lookupTarget(triple, Status);

/*
* If we fail to get the target then we haven't registered it. The
Expand All @@ -84,7 +84,7 @@ EmulateInstructionMIPS::EmulateInstructionMIPS(
LLVMInitializeMipsAsmPrinter();
LLVMInitializeMipsTargetMC();
LLVMInitializeMipsDisassembler();
target = llvm::TargetRegistry::lookupTarget(triple.getTriple(), Status);
target = llvm::TargetRegistry::lookupTarget(triple, Status);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ EmulateInstructionMIPS64::EmulateInstructionMIPS64(
std::string Status;
llvm::Triple triple = arch.GetTriple();
const llvm::Target *target =
llvm::TargetRegistry::lookupTarget(triple.getTriple(), Status);
llvm::TargetRegistry::lookupTarget(triple, Status);

/*
* If we fail to get the target then we haven't registered it. The
Expand All @@ -84,7 +84,7 @@ EmulateInstructionMIPS64::EmulateInstructionMIPS64(
LLVMInitializeMipsAsmPrinter();
LLVMInitializeMipsTargetMC();
LLVMInitializeMipsDisassembler();
target = llvm::TargetRegistry::lookupTarget(triple.getTriple(), Status);
target = llvm::TargetRegistry::lookupTarget(triple, Status);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ int main(int Argc, char *Argv[]) {
ExitOnErr(JITTargetMachineBuilder::detectHost()));
} else {
Builder.setJITTargetMachineBuilder(
JITTargetMachineBuilder(Triple(M.getTargetTriple())));
JITTargetMachineBuilder(M.getTargetTriple()));
}
if (!M.getDataLayout().getStringRepresentation().empty())
Builder.setDataLayout(M.getDataLayout());
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class StaticInitGVIterator {

StaticInitGVIterator(Module &M)
: I(M.global_values().begin()), E(M.global_values().end()),
ObjFmt(Triple(M.getTargetTriple()).getObjectFormat()) {
ObjFmt(M.getTargetTriple().getObjectFormat()) {
if (I != E) {
if (!isStaticInitGlobal(*I))
moveToNextStaticInitGlobal();
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class OpenMPIRBuilder {
/// not have an effect on \p M (see initialize)
OpenMPIRBuilder(Module &M)
: M(M), Builder(M.getContext()), OffloadInfoManager(this),
T(Triple(M.getTargetTriple())) {}
T(M.getTargetTriple()) {}
~OpenMPIRBuilder();

class AtomicInfo : public llvm::AtomicInfo {
Expand Down
12 changes: 7 additions & 5 deletions llvm/include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "llvm/IR/SymbolTableListTraits.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/TargetParser/Triple.h"
#include <cstddef>
#include <cstdint>
#include <iterator>
Expand Down Expand Up @@ -189,8 +190,10 @@ class LLVM_ABI Module {
std::string ModuleID; ///< Human readable identifier for the module
std::string SourceFileName; ///< Original source file name for module,
///< recorded in bitcode.
std::string TargetTriple; ///< Platform target triple Module compiled on
///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
/// Platform target triple Module compiled on
/// Format: (arch)(sub)-(vendor)-(sys)-(abi)
// FIXME: Default construction is not the same as empty triple :(
Triple TargetTriple = Triple("");
NamedMDSymTabType NamedMDSymTab; ///< NamedMDNode names.
DataLayout DL; ///< DataLayout associated with the module
StringMap<unsigned>
Expand Down Expand Up @@ -294,8 +297,7 @@ class LLVM_ABI Module {
const DataLayout &getDataLayout() const { return DL; }

/// Get the target triple which is a string describing the target host.
/// @returns a string containing the target triple.
const std::string &getTargetTriple() const { return TargetTriple; }
const Triple &getTargetTriple() const { return TargetTriple; }

/// Get the global data context.
/// @returns LLVMContext - a container for LLVM's global information
Expand Down Expand Up @@ -338,7 +340,7 @@ class LLVM_ABI Module {
void setDataLayout(const DataLayout &Other);

/// Set the target triple.
void setTargetTriple(StringRef T) { TargetTriple = std::string(T); }
void setTargetTriple(Triple T) { TargetTriple = std::move(T); }

/// Set the module-scope inline assembly blocks.
/// A trailing newline is added if the input doesn't have one.
Expand Down
8 changes: 2 additions & 6 deletions llvm/include/llvm/LTO/legacy/LTOModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,10 @@ struct LTOModule {
std::unique_ptr<Module> takeModule() { return std::move(Mod); }

/// Return the Module's target triple.
const std::string &getTargetTriple() {
return getModule().getTargetTriple();
}
const Triple &getTargetTriple() { return getModule().getTargetTriple(); }

/// Set the Module's target triple.
void setTargetTriple(StringRef Triple) {
getModule().setTargetTriple(Triple);
}
void setTargetTriple(Triple T) { getModule().setTargetTriple(T); }

/// Get the number of symbols
uint32_t getSymbolCount() {
Expand Down
13 changes: 12 additions & 1 deletion llvm/include/llvm/MC/TargetRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,23 @@ struct TargetRegistry {

static iterator_range<iterator> targets();

/// lookupTarget - Lookup a target based on a target triple.
///
/// \param TripleStr - The triple to use for finding a target.
/// \param Error - On failure, an error string describing why no target was
/// found.
// TODO: Drop this in favor of the method accepting Triple.
static const Target *lookupTarget(StringRef TripleStr, std::string &Error) {
return lookupTarget(Triple(TripleStr), Error);
}

/// lookupTarget - Lookup a target based on a target triple.
///
/// \param Triple - The triple to use for finding a target.
/// \param Error - On failure, an error string describing why no target was
/// found.
static const Target *lookupTarget(StringRef Triple, std::string &Error);
static const Target *lookupTarget(const Triple &TheTriple,
std::string &Error);

/// lookupTarget - Lookup a target based on an architecture name
/// and a target triple. If the architecture name is non-empty,
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/TargetParser/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ class Triple {

const std::string &getTriple() const { return Data; }

/// Whether the triple is empty / default constructed.
bool empty() const { return Data.empty(); }

/// Get the architecture (first) component of the triple.
StringRef getArchName() const;

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/DXILMetadataAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using namespace dxil;

static ModuleMetadataInfo collectMetadataInfo(Module &M) {
ModuleMetadataInfo MMDAI;
Triple TT(Triple(M.getTargetTriple()));
const Triple &TT = M.getTargetTriple();
MMDAI.DXILVersion = TT.getDXILVersion();
MMDAI.ShaderModelVersion = TT.getOSVersion();
MMDAI.ShaderProfile = TT.getEnvironment();
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Analysis/Lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Lint : public InstVisitor<Lint> {

public:
Module *Mod;
Triple TT;
const Triple &TT;
const DataLayout *DL;
AliasAnalysis *AA;
AssumptionCache *AC;
Expand All @@ -139,8 +139,8 @@ class Lint : public InstVisitor<Lint> {

Lint(Module *Mod, const DataLayout *DL, AliasAnalysis *AA,
AssumptionCache *AC, DominatorTree *DT, TargetLibraryInfo *TLI)
: Mod(Mod), TT(Triple::normalize(Mod->getTargetTriple())), DL(DL), AA(AA),
AC(AC), DT(DT), TLI(TLI), MessagesStr(Messages) {}
: Mod(Mod), TT(Mod->getTargetTriple()), DL(DL), AA(AA), AC(AC), DT(DT),
TLI(TLI), MessagesStr(Messages) {}

void WriteValues(ArrayRef<const Value *> Vs) {
for (const Value *V : Vs) {
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Analysis/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static bool hasBcmp(const Triple &TT) {
return TT.isOSFreeBSD() || TT.isOSSolaris();
}

static bool isCallingConvCCompatible(CallingConv::ID CC, StringRef TT,
static bool isCallingConvCCompatible(CallingConv::ID CC, const Triple &TT,
FunctionType *FuncTy) {
switch (CC) {
default:
Expand All @@ -131,7 +131,7 @@ static bool isCallingConvCCompatible(CallingConv::ID CC, StringRef TT,

// The iOS ABI diverges from the standard in some cases, so for now don't
// try to simplify those calls.
if (Triple(TT).isiOS())
if (TT.isiOS())
return false;

if (!FuncTy->getReturnType()->isPointerTy() &&
Expand Down Expand Up @@ -1446,8 +1446,7 @@ TargetLibraryInfoImpl::getVectorMappingInfo(StringRef F, const ElementCount &VF,
TargetLibraryInfo TargetLibraryAnalysis::run(const Function &F,
FunctionAnalysisManager &) {
if (!BaselineInfoImpl)
BaselineInfoImpl =
TargetLibraryInfoImpl(Triple(F.getParent()->getTargetTriple()));
BaselineInfoImpl = TargetLibraryInfoImpl(F.getParent()->getTargetTriple());
return TargetLibraryInfo(*BaselineInfoImpl, &F);
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) {
// Run the override callback to potentially change the data layout string, and
// parse the data layout string.
if (auto LayoutOverride =
DataLayoutCallback(M->getTargetTriple(), TentativeDLStr)) {
DataLayoutCallback(M->getTargetTriple().str(), TentativeDLStr)) {
TentativeDLStr = *LayoutOverride;
DLStrLoc = {};
}
Expand Down Expand Up @@ -646,7 +646,7 @@ bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
if (parseToken(lltok::equal, "expected '=' after target triple") ||
parseStringConstant(Str))
return true;
M->setTargetTriple(Str);
M->setTargetTriple(Triple(Str));
return false;
case lltok::kw_datalayout:
Lex.Lex();
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4530,12 +4530,12 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,

// Auto-upgrade the layout string
TentativeDataLayoutStr = llvm::UpgradeDataLayoutString(
TentativeDataLayoutStr, TheModule->getTargetTriple());
TentativeDataLayoutStr, TheModule->getTargetTriple().str());

// Apply override
if (Callbacks.DataLayout) {
if (auto LayoutOverride = (*Callbacks.DataLayout)(
TheModule->getTargetTriple(), TentativeDataLayoutStr))
TheModule->getTargetTriple().str(), TentativeDataLayoutStr))
TentativeDataLayoutStr = *LayoutOverride;
}

Expand Down Expand Up @@ -4719,7 +4719,7 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
std::string S;
if (convertToString(Record, 0, S))
return error("Invalid record");
TheModule->setTargetTriple(S);
TheModule->setTargetTriple(Triple(S));
break;
}
case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
Expand Down
Loading