[clang][nvlink-wrapper] Add support for opt-remarks command line options (#145365)

## Problem
When using `-fsave-optimization-record` with offloading, the Clang
driver passes optimization record options like
`-plugin-opt=opt-remarks-format=yaml` to `clang-nvlink-wrapper`.
However, the wrapper doesn't recognize these options, causing
compilation to fail.


## Solution
This patch adds support for the standard optimization record command
line options to `clang-nvlink-wrapper`, matching the interface provided
by LLD and gold-plugin as documented in the [LLVM Remarks
documentation](https://llvm.org/docs/Remarks.html).

## Changes
- **NVLinkOpts.td**: Added definitions for `opt-remarks-filename`,
`opt-remarks-format`, `opt-remarks-filter`, and
`opt-remarks-with-hotness` options
- **NVLinkOpts.td**: Added `plugin-opt=` aliases for these options to
match what the Clang driver sends
- **ClangNVLinkWrapper.cpp**: Updated `createLTO()` to use command line
arguments when available, falling back to existing global variables

## Testing
The fix allows `-fsave-optimization-record` to work correctly with
offloading, generating optimization records during the LTO phase without
throwing unknown argument errors.

This change maintains backward compatibility and follows the existing
pattern used by other LLVM linkers.
This commit is contained in:
Miguel Cárdenas 2025-06-23 20:15:04 +02:00 committed by GitHub
parent 0f173a0f9a
commit e80acd4fae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View File

@ -341,11 +341,16 @@ Expected<std::unique_ptr<lto::LTO>> createLTO(const ArgList &Args) {
Conf.CPU = Args.getLastArgValue(OPT_arch);
Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple);
Conf.RemarksFilename = RemarksFilename;
Conf.RemarksPasses = RemarksPasses;
Conf.RemarksWithHotness = RemarksWithHotness;
Conf.RemarksFilename =
Args.getLastArgValue(OPT_opt_remarks_filename, RemarksFilename);
Conf.RemarksPasses =
Args.getLastArgValue(OPT_opt_remarks_filter, RemarksPasses);
Conf.RemarksFormat =
Args.getLastArgValue(OPT_opt_remarks_format, RemarksFormat);
Conf.RemarksWithHotness =
Args.hasArg(OPT_opt_remarks_with_hotness) || RemarksWithHotness;
Conf.RemarksHotnessThreshold = RemarksHotnessThreshold;
Conf.RemarksFormat = RemarksFormat;
Conf.MAttrs = llvm::codegen::getMAttrs();
std::optional<CodeGenOptLevel> CGOptLevelOrNone =

View File

@ -72,6 +72,16 @@ def : Joined<["--", "-"], "plugin-opt=emit-llvm">,
Flags<[WrapperOnlyOption]>, Alias<lto_emit_llvm>;
def : Joined<["--", "-"], "plugin-opt=emit-asm">,
Flags<[WrapperOnlyOption]>, Alias<lto_emit_asm>;
def opt_remarks_filename : Joined<["--", "-"], "plugin-opt=opt-remarks-filename=">,
Flags<[WrapperOnlyOption]>, HelpText<"YAML output file for optimization remarks">;
def opt_remarks_format : Joined<["--", "-"], "plugin-opt=opt-remarks-format=">,
Flags<[WrapperOnlyOption]>, HelpText<"The format used for serializing remarks (default: YAML)">;
def opt_remarks_filter : Joined<["--", "-"], "plugin-opt=opt-remarks-filter=">,
Flags<[WrapperOnlyOption]>, HelpText<"Regex for the passes that need to be serialized to the output file">;
def opt_remarks_with_hotness : Flag<["--", "-"], "plugin-opt=opt-remarks-with-hotness">,
Flags<[WrapperOnlyOption]>, HelpText<"Include hotness information in the optimization remarks file">;
def plugin_opt : Joined<["--", "-"], "plugin-opt=">, Flags<[WrapperOnlyOption]>,
HelpText<"Options passed to LLVM, not including the Clang invocation. Use "
"'--plugin-opt=--help' for a list of options.">;