Skip to content

Commit a8f8613

Browse files
committed
Introduce and use codegen::createTargetMachineForTriple()
This creates a TargetMachine with the default options (from the command line flags). This allows us to share a bit more code between tools. Differential Revision: https://reviews.llvm.org/D141057
1 parent d32edcb commit a8f8613

File tree

6 files changed

+47
-79
lines changed

6 files changed

+47
-79
lines changed

llvm/include/llvm/CodeGen/CommandFlags.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Module;
2828
class AttrBuilder;
2929
class Function;
3030
class Triple;
31+
class TargetMachine;
3132

3233
namespace codegen {
3334

@@ -186,6 +187,14 @@ void setFunctionAttributes(StringRef CPU, StringRef Features, Module &M);
186187
/// Should value-tracking variable locations / instruction referencing be
187188
/// enabled by default for this triple?
188189
bool getDefaultValueTrackingVariableLocations(const llvm::Triple &T);
190+
191+
/// Creates a TargetMachine instance with the options defined on the command
192+
/// line. This can be used for tools that do not need further customization of
193+
/// the TargetOptions.
194+
Expected<std::unique_ptr<TargetMachine>> createTargetMachineForTriple(
195+
StringRef TargetTriple,
196+
CodeGenOptLevel OptLevel = CodeGenOptLevel::Default);
197+
189198
} // namespace codegen
190199
} // namespace llvm
191200

llvm/lib/CodeGen/CommandFlags.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include "llvm/IR/Intrinsics.h"
1919
#include "llvm/IR/Module.h"
2020
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
21+
#include "llvm/MC/TargetRegistry.h"
2122
#include "llvm/Support/CommandLine.h"
2223
#include "llvm/Support/MemoryBuffer.h"
24+
#include "llvm/Target/TargetMachine.h"
2325
#include "llvm/TargetParser/Host.h"
2426
#include "llvm/TargetParser/SubtargetFeature.h"
2527
#include "llvm/TargetParser/Triple.h"
@@ -732,3 +734,24 @@ void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
732734
for (Function &F : M)
733735
setFunctionAttributes(CPU, Features, F);
734736
}
737+
738+
Expected<std::unique_ptr<TargetMachine>>
739+
codegen::createTargetMachineForTriple(StringRef TargetTriple,
740+
CodeGenOptLevel OptLevel) {
741+
Triple TheTriple(TargetTriple);
742+
std::string Error;
743+
const auto *TheTarget =
744+
TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
745+
if (!TheTarget)
746+
return createStringError(inconvertibleErrorCode(), Error);
747+
auto *Target = TheTarget->createTargetMachine(
748+
TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
749+
codegen::InitTargetOptionsFromCodeGenFlags(TheTriple),
750+
codegen::getExplicitRelocModel(), codegen::getExplicitCodeModel(),
751+
OptLevel);
752+
if (!Target)
753+
return createStringError(inconvertibleErrorCode(),
754+
Twine("could not allocate target machine for ") +
755+
TargetTriple);
756+
return std::unique_ptr<TargetMachine>(Target);
757+
}

llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,7 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
129129
exit(1);
130130
}
131131

132-
Triple TheTriple = Triple(Triple::normalize(TargetTriple));
133-
134-
// Get the target specific parser.
135-
std::string Error;
136-
const Target *TheTarget =
137-
TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
138-
if (!TheTarget) {
139-
errs() << argv[0] << ": " << Error;
140-
return 1;
141-
}
142-
143132
// Set up the pipeline like llc does.
144-
std::string CPUStr = codegen::getCPUStr(),
145-
FeaturesStr = codegen::getFeaturesStr();
146133

147134
CodeGenOptLevel OLvl;
148135
if (auto Level = CodeGenOpt::parseLevel(OptLevel)) {
@@ -151,11 +138,9 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
151138
errs() << argv[0] << ": invalid optimization level.\n";
152139
return 1;
153140
}
154-
155-
TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
156-
TM.reset(TheTarget->createTargetMachine(
157-
TheTriple.getTriple(), CPUStr, FeaturesStr, Options,
158-
codegen::getExplicitRelocModel(), codegen::getExplicitCodeModel(), OLvl));
141+
ExitOnError ExitOnErr(std::string(*argv[0]) + ": error:");
142+
TM = ExitOnErr(codegen::createTargetMachineForTriple(
143+
Triple::normalize(TargetTriple), OLvl));
159144
assert(TM && "Could not allocate target machine!");
160145

161146
// Make sure we print the summary and the current unit when LLVM errors out.

llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,9 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
198198
errs() << *argv[0] << ": -mtriple must be specified\n";
199199
exit(1);
200200
}
201-
Triple TargetTriple = Triple(Triple::normalize(TargetTripleStr));
202-
203-
std::string Error;
204-
const Target *TheTarget =
205-
TargetRegistry::lookupTarget(codegen::getMArch(), TargetTriple, Error);
206-
if (!TheTarget) {
207-
errs() << *argv[0] << ": " << Error;
208-
exit(1);
209-
}
210-
211-
TargetOptions Options =
212-
codegen::InitTargetOptionsFromCodeGenFlags(TargetTriple);
213-
TM.reset(TheTarget->createTargetMachine(
214-
TargetTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
215-
Options, codegen::getExplicitRelocModel(),
216-
codegen::getExplicitCodeModel(), CodeGenOptLevel::Default));
217-
assert(TM && "Could not allocate target machine!");
201+
ExitOnError ExitOnErr(std::string(*argv[0]) + ": error:");
202+
TM = ExitOnErr(codegen::createTargetMachineForTriple(
203+
Triple::normalize(TargetTripleStr)));
218204

219205
// Check that pass pipeline is specified and correct
220206
//

llvm/tools/llvm-reduce/ReducerWorkItem.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -764,31 +764,17 @@ llvm::parseReducerWorkItem(StringRef ToolName, StringRef Filename,
764764

765765
auto SetDataLayout = [&](StringRef DataLayoutTargetTriple,
766766
StringRef OldDLStr) -> std::optional<std::string> {
767-
// If we are supposed to override the target triple, do so now.
767+
// NB: We always call createTargetMachineForTriple() even if an explicit
768+
// DataLayout is already set in the module since we want to use this
769+
// callback to setup the TargetMachine rather than doing it later.
768770
std::string IRTargetTriple = DataLayoutTargetTriple.str();
769771
if (!TargetTriple.empty())
770772
IRTargetTriple = Triple::normalize(TargetTriple);
771773
TheTriple = Triple(IRTargetTriple);
772774
if (TheTriple.getTriple().empty())
773775
TheTriple.setTriple(sys::getDefaultTargetTriple());
774-
775-
std::string Error;
776-
const Target *TheTarget =
777-
TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
778-
if (!TheTarget) {
779-
WithColor::error(errs(), ToolName) << Error;
780-
exit(1);
781-
}
782-
783-
// Hopefully the MIR parsing doesn't depend on any options.
784-
TargetOptions Options;
785-
std::optional<Reloc::Model> RM = codegen::getExplicitRelocModel();
786-
std::string CPUStr = codegen::getCPUStr();
787-
std::string FeaturesStr = codegen::getFeaturesStr();
788-
TM = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
789-
TheTriple.getTriple(), CPUStr, FeaturesStr, Options, RM,
790-
codegen::getExplicitCodeModel(), CodeGenOptLevel::Default));
791-
assert(TM && "Could not allocate target machine!");
776+
ExitOnError ExitOnErr(std::string(ToolName) + ": error: ");
777+
TM = ExitOnErr(codegen::createTargetMachineForTriple(TheTriple.str()));
792778

793779
return TM->createDataLayout().getStringRepresentation();
794780
};

llvm/tools/opt/opt.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -286,24 +286,6 @@ static CodeGenOptLevel GetCodeGenOptLevel() {
286286
return static_cast<CodeGenOptLevel>(unsigned(CodeGenOptLevelCL));
287287
}
288288

289-
// Returns the TargetMachine instance or zero if no triple is provided.
290-
static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
291-
StringRef FeaturesStr,
292-
const TargetOptions &Options) {
293-
std::string Error;
294-
const Target *TheTarget =
295-
TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
296-
// Some modules don't specify a triple, and this is okay.
297-
if (!TheTarget) {
298-
return nullptr;
299-
}
300-
301-
return TheTarget->createTargetMachine(
302-
TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
303-
Options, codegen::getExplicitRelocModel(),
304-
codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
305-
}
306-
307289
struct TimeTracerRAII {
308290
TimeTracerRAII(StringRef ProgramName) {
309291
if (TimeTrace)
@@ -410,6 +392,7 @@ static bool shouldForceLegacyPM() {
410392
//
411393
int main(int argc, char **argv) {
412394
InitLLVM X(argc, argv);
395+
ExitOnError ExitOnErr(std::string(argv[0]) + ": error: ");
413396

414397
// Enable debug stream buffering.
415398
EnableDebugBuffering = true;
@@ -611,23 +594,19 @@ int main(int argc, char **argv) {
611594

612595
Triple ModuleTriple(M->getTargetTriple());
613596
std::string CPUStr, FeaturesStr;
614-
TargetMachine *Machine = nullptr;
615-
const TargetOptions Options =
616-
codegen::InitTargetOptionsFromCodeGenFlags(ModuleTriple);
617-
597+
std::unique_ptr<TargetMachine> TM;
618598
if (ModuleTriple.getArch()) {
619599
CPUStr = codegen::getCPUStr();
620600
FeaturesStr = codegen::getFeaturesStr();
621-
Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
601+
TM = ExitOnErr(codegen::createTargetMachineForTriple(ModuleTriple.str(),
602+
GetCodeGenOptLevel()));
622603
} else if (ModuleTriple.getArchName() != "unknown" &&
623604
ModuleTriple.getArchName() != "") {
624605
errs() << argv[0] << ": unrecognized architecture '"
625606
<< ModuleTriple.getArchName() << "' provided.\n";
626607
return 1;
627608
}
628609

629-
std::unique_ptr<TargetMachine> TM(Machine);
630-
631610
// Override function attributes based on CPUStr, FeaturesStr, and command line
632611
// flags.
633612
codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);

0 commit comments

Comments
 (0)