Index: llvm/test/tools/llvm-reduce/mir/infer-triple-unknown-target.mir =================================================================== --- /dev/null +++ llvm/test/tools/llvm-reduce/mir/infer-triple-unknown-target.mir @@ -0,0 +1,19 @@ +# RUN: not llvm-reduce --test FileCheck %s -o /dev/null 2>&1 | FileCheck %s + +# CHECK: llvm-reduce: error: unable to get target for 'omgwtfbbqcpu64--', see --version and --triple. + +--- | + target triple = "omgwtfbbqcpu64--" + + define void @func() { + ret void + } + +... +--- +name: func +tracksRegLiveness: true +body: | + bb.0: + +... Index: llvm/test/tools/llvm-reduce/mir/infer-triple.mir =================================================================== --- /dev/null +++ llvm/test/tools/llvm-reduce/mir/infer-triple.mir @@ -0,0 +1,27 @@ +# REQUIRES: amdgpu-registered-target +# RUN: llvm-reduce -simplify-mir --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log +# RUN: FileCheck --check-prefix=RESULT %s < %t + +# Test that the MIR parser successfully infers the target triple from +# the module and avoids needing the -mtriple flag. + +# CHECK-INTERESTINGNESS: S_ENDPGM + +# RESULT: name: func + +--- | + target triple = "amdgcn-amd-amdhsa" + + define void @func() { + ret void + } + +... +--- +name: func +tracksRegLiveness: true +body: | + bb.0: + S_NOP 0 + S_ENDPGM 0 +... Index: llvm/tools/llvm-reduce/ReducerWorkItem.h =================================================================== --- llvm/tools/llvm-reduce/ReducerWorkItem.h +++ llvm/tools/llvm-reduce/ReducerWorkItem.h @@ -12,6 +12,7 @@ #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/IR/Module.h" +#include "llvm/Target/TargetMachine.h" using namespace llvm; @@ -25,9 +26,10 @@ operator MachineFunction &() const { return *MF; } }; -std::unique_ptr parseReducerWorkItem(StringRef Filename, - LLVMContext &Ctxt, - MachineModuleInfo *MMI); +std::unique_ptr +parseReducerWorkItem(const char *ToolName, StringRef Filename, + LLVMContext &Ctxt, std::unique_ptr &TM, + std::unique_ptr &MMI, bool IsMIR); std::unique_ptr cloneReducerWorkItem(const ReducerWorkItem &MMM); Index: llvm/tools/llvm-reduce/ReducerWorkItem.cpp =================================================================== --- llvm/tools/llvm-reduce/ReducerWorkItem.cpp +++ llvm/tools/llvm-reduce/ReducerWorkItem.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "ReducerWorkItem.h" +#include "llvm/CodeGen/CommandFlags.h" #include "llvm/CodeGen/MIRParser/MIRParser.h" #include "llvm/CodeGen/MIRPrinter.h" #include "llvm/CodeGen/MachineDominators.h" @@ -17,10 +18,18 @@ #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" +#include "llvm/MC/TargetRegistry.h" +#include "llvm/Support/Host.h" #include "llvm/Support/SourceMgr.h" +#include "llvm/Support/WithColor.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Transforms/Utils/Cloning.h" +extern cl::OptionCategory LLVMReduceOptions; +static cl::opt TargetTriple("mtriple", + cl::desc("Set the target triple"), + cl::cat(LLVMReduceOptions)); + static void cloneFrameInfo( MachineFrameInfo &DstMFI, const MachineFrameInfo &SrcMFI, const DenseMap &Src2DstMBB) { @@ -235,21 +244,52 @@ return DstMF; } -std::unique_ptr parseReducerWorkItem(StringRef Filename, - LLVMContext &Ctxt, - MachineModuleInfo *MMI) { +std::unique_ptr +parseReducerWorkItem(const char *ToolName, StringRef Filename, + LLVMContext &Ctxt, std::unique_ptr &TM, + std::unique_ptr &MMI, bool IsMIR) { + Triple TheTriple; + auto MMM = std::make_unique(); - if (MMI) { + + if (IsMIR) { auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true); std::unique_ptr MParser = createMIRParser(std::move(FileOrErr.get()), Ctxt); auto SetDataLayout = [&](StringRef DataLayoutTargetTriple) -> Optional { - return MMI->getTarget().createDataLayout().getStringRepresentation(); + // If we are supposed to override the target triple, do so now. + std::string IRTargetTriple = DataLayoutTargetTriple.str(); + if (!TargetTriple.empty()) + IRTargetTriple = Triple::normalize(TargetTriple); + TheTriple = Triple(IRTargetTriple); + if (TheTriple.getTriple().empty()) + TheTriple.setTriple(sys::getDefaultTargetTriple()); + + std::string Error; + const Target *TheTarget = + TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error); + if (!TheTarget) { + WithColor::error(errs(), ToolName) << Error; + exit(1); + } + + // Hopefully the MIR parsing doesn't depend on any options. + TargetOptions Options; + Optional RM = codegen::getExplicitRelocModel(); + std::string CPUStr = codegen::getCPUStr(); + std::string FeaturesStr = codegen::getFeaturesStr(); + TM = std::unique_ptr(TheTarget->createTargetMachine( + TheTriple.getTriple(), CPUStr, FeaturesStr, Options, RM, + codegen::getExplicitCodeModel(), CodeGenOpt::Default)); + assert(TM && "Could not allocate target machine!"); + + return TM->createDataLayout().getStringRepresentation(); }; std::unique_ptr M = MParser->parseIRModule(SetDataLayout); + MMI = std::make_unique(TM.get()); MParser->parseMachineFunctions(*M, *MMI); MachineFunction *MF = nullptr; for (auto &F : *M) { @@ -273,13 +313,14 @@ SMDiagnostic Err; std::unique_ptr Result = parseIRFile(Filename, Err, Ctxt); if (!Result) { - Err.print("llvm-reduce", errs()); + Err.print(ToolName, errs()); return std::unique_ptr(); } MMM->M = std::move(Result); } if (verifyReducerWorkItem(*MMM, &errs())) { - errs() << "Error: " << Filename << " - input module is broken!\n"; + WithColor::error(errs(), ToolName) + << Filename << " - input module is broken!\n"; return std::unique_ptr(); } return MMM; Index: llvm/tools/llvm-reduce/llvm-reduce.cpp =================================================================== --- llvm/tools/llvm-reduce/llvm-reduce.cpp +++ llvm/tools/llvm-reduce/llvm-reduce.cpp @@ -28,9 +28,8 @@ #include "llvm/Support/InitLLVM.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/TargetSelect.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/WithColor.h" -#include "llvm/Target/TargetMachine.h" +#include "llvm/Support/raw_ostream.h" #include #include @@ -85,10 +84,6 @@ clEnumValN(InputLanguages::MIR, "mir", "")), cl::cat(LLVMReduceOptions)); -static cl::opt TargetTriple("mtriple", - cl::desc("Set the target triple"), - cl::cat(LLVMReduceOptions)); - static cl::opt MaxPassIterations("max-pass-iterations", cl::desc("Maximum number of times to run the full set " @@ -97,6 +92,13 @@ static codegen::RegisterCodeGenFlags CGF; +static void initializeTargetInfo() { + InitializeAllTargets(); + InitializeAllTargetMCs(); + InitializeAllAsmPrinters(); + InitializeAllAsmParsers(); +} + void writeOutput(ReducerWorkItem &M, StringRef Message) { if (ReplaceInput) // In-place OutputFilename = InputFilename.c_str(); @@ -112,26 +114,6 @@ errs() << Message << OutputFilename << "\n"; } -static std::unique_ptr createTargetMachine() { - InitializeAllTargets(); - InitializeAllTargetMCs(); - InitializeAllAsmPrinters(); - InitializeAllAsmParsers(); - - if (TargetTriple == "") - TargetTriple = sys::getDefaultTargetTriple(); - auto TT(Triple::normalize(TargetTriple)); - std::string CPU(codegen::getCPUStr()); - std::string FS(codegen::getFeaturesStr()); - - std::string Error; - const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error); - - return std::unique_ptr( - static_cast(TheTarget->createTargetMachine( - TT, CPU, FS, TargetOptions(), None, None, CodeGenOpt::Default))); -} - int main(int Argc, char **Argv) { InitLLVM X(Argc, Argv); @@ -151,15 +133,15 @@ return 0; } + if (ReduceModeMIR) + initializeTargetInfo(); + LLVMContext Context; - std::unique_ptr TM; + std::unique_ptr TM; std::unique_ptr MMI; - std::unique_ptr OriginalProgram; - if (ReduceModeMIR) { - TM = createTargetMachine(); - MMI = std::make_unique(TM.get()); - } - OriginalProgram = parseReducerWorkItem(InputFilename, Context, MMI.get()); + + std::unique_ptr OriginalProgram = parseReducerWorkItem( + Argv[0], InputFilename, Context, TM, MMI, ReduceModeMIR); if (!OriginalProgram) { return 1; }