Skip to content

Commit

Permalink
[lld] Add flag to specify a Cilk tool to use, such as Cilkscale, that…
Browse files Browse the repository at this point in the history
… requires instrumentation during or after Tapir lowering.
  • Loading branch information
neboat committed Dec 7, 2024
1 parent 0bab07e commit 80676cb
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lld/COFF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ struct Configuration {
llvm::StringRef opencilkABIBitcodeFile;
llvm::TapirTargetID tapirTarget = llvm::TapirTargetID::None;

// Used for Cilk tool.
llvm::StringRef cilktool;

// Used for /thinlto-index-only:
llvm::StringRef thinLTOIndexOnlyArg;

Expand Down
2 changes: 2 additions & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ static std::string createResponseFile(const opt::InputArgList &args,
case OPT_winsysroot:
break;
case OPT_call_graph_ordering_file:
case OPT_cilktool:
case OPT_deffile:
case OPT_manifestinput:
case OPT_natvis:
Expand Down Expand Up @@ -2067,6 +2068,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
args.getLastArgValue(OPT_opencilk_abi_bitcode);
config->tapirTarget =
args::parseTapirTarget(args.getLastArgValue(OPT_tapir_target));
config->cilktool = args.getLastArgValue(OPT_cilktool);

if (config->incremental && args.hasArg(OPT_profile)) {
warn("ignoring '/incremental' due to '/profile' specification");
Expand Down
1 change: 1 addition & 0 deletions lld/COFF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ lto::Config BitcodeCompiler::createConfig() {
if (args::validTapirTarget(ctx.config.tapirTarget))
c.TapirTarget = ctx.config.tapirTarget;
c.OpenCilkABIBitcodeFile = std::string(ctx.config.opencilkABIBitcodeFile);
c.Cilktool = std::string(ctx.config.cilktool);
c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile);
c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate;
c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch;
Expand Down
1 change: 1 addition & 0 deletions lld/COFF/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def help_q : Flag<["/??", "-??", "/?", "-?"], "">, Alias<help>;
// LLD extensions
defm auto_import : B_priv<"auto-import">;
defm runtime_pseudo_reloc : B_priv<"runtime-pseudo-reloc">;
def cilktool : P<"cilktool", "Name of Cilk tool">;
def end_lib : F<"end-lib">,
HelpText<"End group of objects treated as if they were in a library">;
def exclude_all_symbols : F<"exclude-all-symbols">;
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct Config {
llvm::StringMap<uint64_t> sectionStartMap;
llvm::StringRef bfdname;
llvm::StringRef chroot;
llvm::StringRef cilktool;
llvm::StringRef dependencyFile;
llvm::StringRef dwoDir;
llvm::StringRef dynamicLinker;
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ static void readConfigs(opt::InputArgList &args) {
config->checkSections =
args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
config->chroot = args.getLastArgValue(OPT_chroot);
config->cilktool = args.getLastArgValue(OPT_cilktool);
config->compressDebugSections = getCompressionType(
args.getLastArgValue(OPT_compress_debug_sections, "none"),
"--compress-debug-sections");
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ std::string elf::createResponseFile(const opt::InputArgList &args) {
os << arg->getSpelling() << quote(rewritePath(arg->getValue())) << "\n";
break;
case OPT_call_graph_ordering_file:
case OPT_cilktool:
case OPT_dynamic_list:
case OPT_export_dynamic_symbol_list:
case OPT_just_symbols:
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ static lto::Config createConfig() {
if (args::validTapirTarget(config->tapirTarget))
c.TapirTarget = config->tapirTarget;
c.OpenCilkABIBitcodeFile = std::string(config->opencilkABIBitcodeFile);
c.Cilktool = std::string(config->cilktool);

c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
c.ValidateAllVtablesHaveTypeInfos =
Expand Down
4 changes: 4 additions & 0 deletions lld/ELF/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ defm lto_validate_all_vtables_have_type_infos: BB<"lto-validate-all-vtables-have
defm lto_whole_program_visibility: BB<"lto-whole-program-visibility",
"Asserts that the LTO link has whole program visibility",
"Asserts that the LTO link does not have whole program visibility">;
defm cilktool: Eq<"cilktool", "Name of Cilk tool">;
def disable_verify: F<"disable-verify">;
defm mllvm: Eq<"mllvm", "Additional arguments to forward to LLVM's option processing">;
defm opencilk_abi_bitcode: EEq<"opencilk-abi-bitcode", "Path to OpenCilk ABI bitcode file">;
Expand Down Expand Up @@ -683,6 +684,9 @@ def: J<"plugin-opt=cs-profile-path=">,
def: J<"plugin-opt=obj-path=">,
Alias<lto_obj_path_eq>,
HelpText<"Alias for --lto-obj-path=">;
def: J<"plugin-opt=cilktool=">,
Alias<cilktool>,
HelpText<"Alias for --cilktool">;
def: J<"plugin-opt=opencilk-abi-bitcode=">,
Alias<opencilk_abi_bitcode>,
HelpText<"Alias for --opencilk-abi-bitcode">;
Expand Down
1 change: 1 addition & 0 deletions lld/MachO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ struct Configuration {

llvm::TapirTargetID tapirTarget = llvm::TapirTargetID::None;
llvm::StringRef opencilkABIBitcodeFile;
llvm::StringRef cilktool;

llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }

Expand Down
1 change: 1 addition & 0 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
args::parseTapirTarget(args.getLastArgValue(OPT_tapir_target));
config->opencilkABIBitcodeFile =
args.getLastArgValue(OPT_opencilk_abi_bitcode);
config->cilktool = args.getLastArgValue(OPT_cilktool);

for (const Arg *arg : args.filtered(OPT_alias)) {
config->aliasedSymbols.push_back(
Expand Down
1 change: 1 addition & 0 deletions lld/MachO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static lto::Config createConfig() {
if (args::validTapirTarget(config->tapirTarget))
c.TapirTarget = config->tapirTarget;
c.OpenCilkABIBitcodeFile = std::string(config->opencilkABIBitcodeFile);
c.Cilktool = std::string(config->cilktool);
if (config->saveTemps)
checkError(c.addSaveTemps(config->outputFile.str() + ".",
/*UseInputModulePath=*/true));
Expand Down
3 changes: 3 additions & 0 deletions lld/MachO/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def tapir_target: Joined<["--"], "tapir-target=">,
def opencilk_abi_bitcode: Joined<["--"], "opencilk-abi-bitcode=">,
HelpText<"Path to the OpenCilk ABI bitcode file">,
Group<grp_lld>;
def cilktool: Joined<["--"], "cilktool=">,
HelpText<"Name of Cilk tool">,
Group<grp_lld>;

// This is a complete Options.td compiled from Apple's ld(1) manpage
// dated 2018-03-07 and cross checked with ld64 source code in repo
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/LTO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ struct Config {
// Path to OpenCilk runtime bitcode file.
std::string OpenCilkABIBitcodeFile;

// Name of Cilk tool.
std::string Cilktool;

/// If this field is set, the set of passes run in the middle-end optimizer
/// will be the one specified by the string. Only works with the new pass
/// manager as the old one doesn't have this ability.
Expand Down
50 changes: 50 additions & 0 deletions llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
#include "llvm/Transforms/Instrumentation/ComprehensiveStaticInstrumentation.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
#include "llvm/Transforms/Utils/SplitModule.h"
Expand Down Expand Up @@ -239,6 +240,29 @@ static bool hasTapirTarget(const Config &Conf) {
(Conf.TapirTarget != TapirTargetID::None);
}

enum class CilktoolID { Cilkscale, CilkscaleInstructions, Unknown };

static CilktoolID parseCilktool(StringRef ToolName) {
return llvm::StringSwitch<CilktoolID>(ToolName)
.Case("cilkscale", CilktoolID::Cilkscale)
.Case("cilkscale-instructions", CilktoolID::CilkscaleInstructions)
.Default(CilktoolID::Unknown);
}

static CSIOptions getCSIOptionsForCilkscale(bool InstrumentBasicBlocks) {
CSIOptions Options;
// Disable CSI hooks that Cilkscale doesn't need.
Options.InstrumentBasicBlocks = InstrumentBasicBlocks;
Options.InstrumentLoops = false;
Options.InstrumentMemoryAccesses = false;
Options.InstrumentCalls = false;
Options.InstrumentAtomics = false;
Options.InstrumentMemIntrinsics = false;
Options.InstrumentAllocas = false;
Options.InstrumentAllocFns = false;
return Options;
}

static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
unsigned OptLevel, bool IsThinLTO,
ModuleSummaryIndex *ExportSummary,
Expand Down Expand Up @@ -329,6 +353,32 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
break;
}

// Handle tools with late CSI instrumentation passes.
switch (parseCilktool(Conf.Cilktool)) {
case CilktoolID::Cilkscale: {
PB.registerTapirLoopEndEPCallback(
[&PB](ModulePassManager &MPM, OptimizationLevel Level) {
MPM.addPass(CSISetupPass(getCSIOptionsForCilkscale(false)));
MPM.addPass(ComprehensiveStaticInstrumentationPass(
getCSIOptionsForCilkscale(false)));
MPM.addPass(PB.buildPostCilkInstrumentationPipeline(Level));
});
break;
}
case CilktoolID::CilkscaleInstructions: {
PB.registerTapirLoopEndEPCallback(
[&PB](ModulePassManager &MPM, OptimizationLevel Level) {
MPM.addPass(CSISetupPass(getCSIOptionsForCilkscale(true)));
MPM.addPass(ComprehensiveStaticInstrumentationPass(
getCSIOptionsForCilkscale(true)));
MPM.addPass(PB.buildPostCilkInstrumentationPipeline(Level));
});
break;
}
case CilktoolID::Unknown:
break;
}

// Parse a custom pipeline if asked to.
if (!Conf.OptPipeline.empty()) {
if (auto Err = PB.parsePassPipeline(MPM, Conf.OptPipeline)) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/tools/gold/gold-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ namespace options {
// Tapir lowering options.
static TapirTargetID tapir_target = TapirTargetID::Last_TapirTargetID;
static std::string opencilk_abi_bitcode_file;
static std::string cilktool;

static TapirTargetID parseTapirTarget(StringRef tapirTarget) {
return llvm::StringSwitch<TapirTargetID>(tapirTarget)
Expand Down Expand Up @@ -330,6 +331,8 @@ namespace options {
tapir_target = parseTapirTarget(std::string(opt));
} else if (opt.consume_front("opencilk-abi-bitcode=")) {
opencilk_abi_bitcode_file = std::string(opt);
} else if (opt.consume_front("cilktool=")) {
cilktool = std::string(opt);
} else {
// Save this option to pass to the code generator.
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
Expand Down Expand Up @@ -979,6 +982,8 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
Conf.OpenCilkABIBitcodeFile = options::opencilk_abi_bitcode_file;
}

Conf.Cilktool = options::cilktool;

return std::make_unique<LTO>(std::move(Conf), Backend,
options::ParallelCodeGenParallelismLevel);
}
Expand Down

0 comments on commit 80676cb

Please sign in to comment.