@@ -89,6 +89,7 @@ cl::OptionCategory PostLinkCat{"sycl-post-link options"};
8989// clang/lib/Driver/Driver.cpp, sycl-post-link.cpp, ClangOffloadWrapper.cpp
9090constexpr char COL_CODE[] = " Code" ;
9191constexpr char COL_SYM[] = " Symbols" ;
92+ constexpr char COL_OPTS[] = " Options" ;
9293constexpr char COL_PROPS[] = " Properties" ;
9394
9495// InputFilename - The filename to read from.
@@ -215,10 +216,11 @@ struct GlobalBinImageProps {
215216 bool EmitDeviceGlobalPropSet;
216217};
217218
218- struct IrPropSymFilenameTriple {
219+ struct IrPropSymFilenameQuad {
219220 std::string Ir;
220221 std::string Prop;
221222 std::string Sym;
223+ std::string Opt;
222224};
223225
224226void writeToFile (const std::string &Filename, const std::string &Content) {
@@ -466,6 +468,44 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
466468 return SCFile;
467469}
468470
471+ std::string getOptString (module_split::ModuleDesc &MD) {
472+ auto &M = MD.getModule ();
473+ // Process all properties on kernels.
474+ for (Function &F : M) {
475+ // Only consider kernels.
476+ if (F.getCallingConv () != CallingConv::SPIR_KERNEL)
477+ continue ;
478+
479+ SmallVector<Metadata *, 8 > MDOps;
480+ SmallVector<std::pair<std::string, MDNode *>, 8 > NamedMDOps;
481+ for (const Attribute &Attr : F.getAttributes ().getFnAttrs ()) {
482+ // Currently, only string attributes are supported
483+ if (!Attr.isStringAttribute ())
484+ continue ;
485+ StringRef AttrKindStr = Attr.getKindAsString ();
486+ if (AttrKindStr == " sycl-device-compile-optlevel" ) {
487+ auto Opt = " -O" + Attr.getValueAsString ();
488+ llvm::errs () << " ARV: Opt is " << Opt << " \n " ;
489+ return Opt.str ();
490+ }
491+ }
492+ }
493+ return " " ;
494+ }
495+
496+ std::string saveModuleOptions (module_split::ModuleDesc &MD,
497+ const std::string &Opts, int I,
498+ StringRef Suff) {
499+ std::error_code EC;
500+ std::string SCFile = makeResultFileName (" .opt" , I, Suff);
501+ raw_fd_ostream SCOut (SCFile, EC);
502+ checkError (EC, " error opening file '" + SCFile + " '" );
503+ SCOut << Opts;
504+
505+ return SCFile;
506+ }
507+
508+
469509// Saves specified collection of symbols to a file.
470510std::string saveModuleSymbolTable (const module_split::EntryPointSet &Es, int I,
471511 StringRef Suffix) {
@@ -570,11 +610,11 @@ StringRef getModuleSuffix(const module_split::ModuleDesc &MD) {
570610// @param IRFilename filename of already available IR component. If not empty,
571611// IR component saving is skipped, and this file name is recorded as such in
572612// the result.
573- // @return a triple of files where IR, Property and Symbols components of the
574- // Module descriptor are written respectively.
575- IrPropSymFilenameTriple saveModule (module_split::ModuleDesc &MD, int I,
613+ // @return a quadruple of files where IR, Property, Symbols and Opts components
614+ // of the Module descriptor are written respectively.
615+ IrPropSymFilenameQuad saveModule (module_split::ModuleDesc &MD, int I,
576616 StringRef IRFilename = " " ) {
577- IrPropSymFilenameTriple Res;
617+ IrPropSymFilenameQuad Res;
578618 StringRef Suffix = getModuleSuffix (MD);
579619
580620 if (!IRFilename.empty ()) {
@@ -587,6 +627,9 @@ IrPropSymFilenameTriple saveModule(module_split::ModuleDesc &MD, int I,
587627 EmitExportedSymbols, DeviceGlobals};
588628 Res.Prop = saveModuleProperties (MD, Props, I, Suffix);
589629
630+ std::string Opts = getOptString (MD);
631+ Res.Opt = saveModuleOptions (MD, Opts, I, Suffix);
632+
590633 if (DoSymGen) {
591634 // save the names of the entry points - the symbol table
592635 Res.Sym = saveModuleSymbolTable (MD.entries (), I, Suffix);
@@ -631,20 +674,37 @@ bool processSpecConstants(module_split::ModuleDesc &MD) {
631674 return MD.Props .SpecConstsMet ;
632675}
633676
634- constexpr int MAX_COLUMNS_IN_FILE_TABLE = 3 ;
677+ constexpr int MAX_COLUMNS_IN_FILE_TABLE = 4 ;
635678
679+ #if 0
636680void addTableRow(util::SimpleTable &Table,
637- const IrPropSymFilenameTriple &RowData) {
681+ const IrPropSymFilenameQuad &RowData) {
638682 SmallVector<StringRef, MAX_COLUMNS_IN_FILE_TABLE> Row;
639683
684+ for (const std::string *S : {&RowData.Ir, &RowData.Prop, &RowData.Opt, &RowData.Sym}) {
685+ if (!S->empty()) {
686+ Row.push_back(StringRef(*S));
687+ }
688+ }
689+ llvm::errs() << "ARV: " << static_cast<size_t>(Table.getNumColumns()) << "," << Row.size() <<"\n";
690+ assert(static_cast<size_t>(Table.getNumColumns()) == Row.size());
691+ Table.addRow(Row);
692+ }
693+ #else
694+ void addTableRow (util::SimpleTable &Table,
695+ const IrPropSymFilenameQuad &RowData) {
696+ SmallVector<StringRef, 3 > Row;
697+
640698 for (const std::string *S : {&RowData.Ir , &RowData.Prop , &RowData.Sym }) {
641699 if (!S->empty ()) {
642700 Row.push_back (StringRef (*S));
643701 }
644702 }
703+ // llvm::errs() << "ARV: " << static_cast<size_t>(Table.getNumColumns()) << "," << Row.size() <<"\n";
645704 assert (static_cast <size_t >(Table.getNumColumns ()) == Row.size ());
646705 Table.addRow (Row);
647706}
707+ #endif
648708
649709// Removes the global variable "llvm.used" and returns true on success.
650710// "llvm.used" is a global constant array containing references to kernels
@@ -693,8 +753,13 @@ static bool removeSYCLKernelsConstRefArray(Module &M) {
693753std::unique_ptr<util::SimpleTable>
694754processInputModule (std::unique_ptr<Module> M) {
695755 // Construct the resulting table which will accumulate all the outputs.
756+ #if 0
757+ SmallVector<StringRef, MAX_COLUMNS_IN_FILE_TABLE> ColumnTitles{
758+ StringRef(COL_CODE), StringRef(COL_PROPS), StringRef(COL_OPTS)};
759+ #else
696760 SmallVector<StringRef, MAX_COLUMNS_IN_FILE_TABLE> ColumnTitles{
697761 StringRef (COL_CODE), StringRef (COL_PROPS)};
762+ #endif
698763
699764 if (DoSymGen) {
700765 ColumnTitles.push_back (COL_SYM);
@@ -869,7 +934,7 @@ processInputModule(std::unique_ptr<Module> M) {
869934 " have been made\n " ;
870935 }
871936 for (module_split::ModuleDesc &IrMD : MMs) {
872- IrPropSymFilenameTriple T = saveModule (IrMD, ID, OutIRFileName);
937+ IrPropSymFilenameQuad T = saveModule (IrMD, ID, OutIRFileName);
873938 addTableRow (*Table, T);
874939 }
875940 }
0 commit comments