@@ -37,6 +37,7 @@ IN THE SOFTWARE.
3737#include " llvm/GenXIntrinsics/GenXIntrinsics.h"
3838#include " llvm/GenXIntrinsics/GenXSPIRVReaderAdaptor.h"
3939
40+ #include " llvm/ADT/ScopeExit.h"
4041#include " llvm/ADT/SmallString.h"
4142#include " llvm/ADT/SmallVector.h"
4243#include " llvm/ADT/StringExtras.h"
@@ -398,11 +399,31 @@ static vc::CompileOutput runCodeGen(const vc::CompileOptions &Opts,
398399 IGC_ASSERT_EXIT_MESSAGE (0 , " Unknown runtime kind" );
399400}
400401
402+ // Parse global llvm cl options.
403+ // Parsing of cl options should not fail under any circumstances.
404+ static void parseLLVMOptions (const std::string &Args) {
405+ BumpPtrAllocator Alloc;
406+ StringSaver Saver{Alloc};
407+ SmallVector<const char *, 8 > Argv{" vc-codegen" };
408+ cl::TokenizeGNUCommandLine (Args, Saver, Argv);
409+
410+ // Reset all options to ensure that scalar part does not affect
411+ // vector compilation.
412+ cl::ResetAllOptionOccurrences ();
413+ cl::ParseCommandLineOptions (Argv.size (), Argv.data ());
414+ }
415+
401416Expected<vc::CompileOutput> vc::Compile (ArrayRef<char > Input,
402417 const vc::CompileOptions &Opts,
403418 const vc::ExternalData &ExtData,
404419 ArrayRef<uint32_t > SpecConstIds,
405420 ArrayRef<uint64_t > SpecConstValues) {
421+ parseLLVMOptions (Opts.LLVMOptions );
422+ // Reset options when everything is done here. This is needed to not
423+ // interfere with subsequent translations (including scalar part).
424+ const auto ClOptGuard =
425+ llvm::make_scope_exit ([]() { cl::ResetAllOptionOccurrences (); });
426+
406427 if (Opts.DumpIR && Opts.Dumper )
407428 Opts.Dumper ->dumpBinary (Input, " input.spv" );
408429
@@ -638,82 +659,57 @@ static Error fillInternalOptions(const opt::ArgList &InternalOptions,
638659 return Error::success ();
639660}
640661
641- static Expected<vc::CompileOptions>
642- fillOptions (const opt::ArgList &ApiOptions,
643- const opt::ArgList &InternalOptions) {
644- vc::CompileOptions Opts;
645- Error Status = fillApiOptions (ApiOptions, Opts);
646- if (Status)
647- return {std::move (Status)};
648-
649- Status = fillInternalOptions (InternalOptions, Opts);
650- if (Status)
651- return {std::move (Status)};
652-
653- return {std::move (Opts)};
654- }
655-
656- // Parse global llvm cl options.
657- // Parsing of cl codegen options should not fail under any circumstances.
658- static void parseLLVMOptions (const opt::ArgList &Args) {
659- // Need to control cl options as vector compiler still uses these ones
660- // to control compilation process. This will be addressed later.
661- llvm::cl::ResetAllOptionOccurrences ();
662- BumpPtrAllocator Alloc;
663- StringSaver Saver{Alloc};
664- SmallVector<const char *, 8 > Argv{" vc-codegen" };
665- for (const std::string &ArgPart :
666- Args.getAllArgValues (IGC::options::OPT_llvm_options))
667- cl::TokenizeGNUCommandLine (ArgPart, Saver, Argv);
668- cl::ParseCommandLineOptions (Argv.size (), Argv.data ());
669- }
670-
671- // Derive llvm options from different API and internal options.
672- static opt::DerivedArgList
673- composeLLVMArgs (const opt::InputArgList &ApiArgs,
674- const opt::InputArgList &InternalArgs,
675- llvm::StringSaver &Saver) {
676- const opt::OptTable &Options = IGC::getOptTable ();
677- const opt::Option LLVMOpt = Options.getOption (IGC::options::OPT_llvm_options);
662+ // Prepare llvm options string using different API and internal options.
663+ static std::string composeLLVMArgs (const opt::ArgList &ApiArgs,
664+ const opt::ArgList &InternalArgs) {
665+ std::string Result;
678666
679- // Pass through old value.
680- opt::DerivedArgList UpdatedArgs{InternalArgs};
667+ // Handle input llvm options.
681668 if (const opt::Arg *BaseArg =
682669 InternalArgs.getLastArg (IGC::options::OPT_llvm_options))
683- UpdatedArgs. AddSeparateArg (BaseArg, LLVMOpt, BaseArg->getValue () );
670+ Result += BaseArg->getValue ();
684671
685672 // Add visaopts if any.
686673 for (auto OptID :
687674 {IGC::options::OPT_igcmc_visaopts, IGC::options::OPT_Xfinalizer}) {
688675 if (!ApiArgs.hasArg (OptID))
689676 continue ;
690-
691- const std::string FinalizerOpts =
692- llvm::join (ApiArgs.getAllArgValues (OptID), " " );
693- StringRef WrappedOpts =
694- Saver.save (Twine{" -finalizer-opts='" } + FinalizerOpts + " '" );
695- UpdatedArgs.AddSeparateArg (ApiArgs.getLastArg (OptID), LLVMOpt, WrappedOpts);
677+ Result += " -finalizer-opts='" ;
678+ Result += join (ApiArgs.getAllArgValues (OptID), " " );
679+ Result += " '" ;
696680 }
697681
698- if (opt::Arg *GTPinReRa = ApiArgs.getLastArg (IGC::options::OPT_gtpin_rera)) {
699- UpdatedArgs.AddSeparateArg (GTPinReRa, LLVMOpt,
700- " -finalizer-opts='-GTPinReRA'" );
701- }
702- if (opt::Arg *GTPinFreeGRFInfo =
703- ApiArgs.getLastArg (IGC::options::OPT_gtpin_grf_info)) {
704- UpdatedArgs.AddSeparateArg (GTPinFreeGRFInfo, LLVMOpt,
705- " -finalizer-opts='-getfreegrfinfo -rerapostschedule'" );
706- }
707- if (opt::Arg *GTPinScratchAreaSize =
682+ // Add gtpin options if any.
683+ if (ApiArgs.hasArg (IGC::options::OPT_gtpin_rera))
684+ Result += " -finalizer-opts='-GTPinReRA'" ;
685+ if (ApiArgs.hasArg (IGC::options::OPT_gtpin_grf_info))
686+ Result += " -finalizer-opts='-getfreegrfinfo -rerapostschedule'" ;
687+ if (opt::Arg *A =
708688 ApiArgs.getLastArg (IGC::options::OPT_gtpin_scratch_area_size)) {
709- StringRef ScratchRef =
710- Saver.save (GTPinScratchAreaSize->getAsString (ApiArgs));
711- auto s = " -finalizer-opts='-GTPinScratchAreaSize " +
712- std::string (GTPinScratchAreaSize->getValue ()) + " '" ;
713- UpdatedArgs.AddSeparateArg (GTPinScratchAreaSize, LLVMOpt, s);
689+ Result += " -finalizer-opts='-GTPinScratchAreaSize " ;
690+ Result += A->getValue ();
691+ Result += " '" ;
714692 }
715693
716- return UpdatedArgs;
694+ return Result;
695+ }
696+
697+ static Expected<vc::CompileOptions>
698+ fillOptions (const opt::ArgList &ApiOptions,
699+ const opt::ArgList &InternalOptions) {
700+ vc::CompileOptions Opts;
701+ Error Status = fillApiOptions (ApiOptions, Opts);
702+ if (Status)
703+ return {std::move (Status)};
704+
705+ Status = fillInternalOptions (InternalOptions, Opts);
706+ if (Status)
707+ return {std::move (Status)};
708+
709+ // Prepare additional llvm options (like finalizer args).
710+ Opts.LLVMOptions = composeLLVMArgs (ApiOptions, InternalOptions);
711+
712+ return {std::move (Opts)};
717713}
718714
719715llvm::Expected<vc::CompileOptions>
@@ -731,12 +727,5 @@ vc::ParseOptions(llvm::StringRef ApiOptions, llvm::StringRef InternalOptions,
731727 return ExpInternalArgList.takeError ();
732728 const opt::InputArgList &InternalArgs = ExpInternalArgList.get ();
733729
734- // Prepare additional llvm options (like finalizer args).
735- opt::DerivedArgList LLVMArgs = composeLLVMArgs (ApiArgs, InternalArgs, Saver);
736-
737- // This is a temporary solution until we remove all cl options that
738- // are accesible by user and affect compilation.
739- parseLLVMOptions (LLVMArgs);
740-
741730 return fillOptions (ApiArgs, InternalArgs);
742731}
0 commit comments