From 556b0fb903481ef66a11efeaa7f949b939f2ef3d Mon Sep 17 00:00:00 2001 From: Elizabeth Andrews Date: Wed, 18 May 2022 08:06:27 -0700 Subject: [PATCH] [SYCL] Restrict collection of information for optimization record Collect information for optimization record only if the optmization record is saved by user (i.e. -fsave-optimization-record or -opt-record-file is passed). This patch prevents unecessary calls to opt-report handlers when not required. I couldn't think of a way to add a test for this since there is no change in output. Information is collected when the flag is passed and emitted into optimization record. Information is no longer collected when there is no optimization record. This is a follow up to https://github.com/intel/llvm/pull/3492 and https://github.com/intel/llvm/pull/3730/ Signed-off-by: Elizabeth Andrews --- clang/include/clang/Basic/LangOptions.h | 4 +++ clang/include/clang/Driver/Options.td | 2 +- clang/lib/CodeGen/CodeGenFunction.cpp | 41 ++++++++++++----------- clang/lib/Frontend/CompilerInvocation.cpp | 10 ++++++ clang/lib/Sema/SemaSYCL.cpp | 22 +++++++++--- 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index 2fc2c2413a3d4..bf5c2468a62df 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -470,6 +470,10 @@ class LangOptions : public LangOptionsBase { /// The seed used by the randomize structure layout feature. std::string RandstructSeed; + /// The name of the file to which the backend should save YAML optimization + /// records. + std::string OptRecordFile; + LangOptions(); /// Set language defaults for the given input language and diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index f0d8b8cf87d87..675e94e65e36c 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5897,7 +5897,7 @@ def arcmt_action_EQ : Joined<["-"], "arcmt-action=">, Flags<[CC1Option, NoDriver def opt_record_file : Separate<["-"], "opt-record-file">, HelpText<"File name to use for YAML optimization record output">, - MarshallingInfoString>; + MarshallingInfoString>; def opt_record_passes : Separate<["-"], "opt-record-passes">, HelpText<"Only record remark information for passes whose names match the given regular expression">; def opt_record_format : Separate<["-"], "opt-record-format">, diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index ea65b5ec8d68d..d15fdb0bf3c51 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1602,26 +1602,27 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, // Emit the standard function prologue. StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin()); - - SyclOptReportHandler &SyclOptReport = CGM.getDiags().getSYCLOptReport(); - if (Fn && SyclOptReport.HasOptReportInfo(FD)) { - llvm::OptimizationRemarkEmitter ORE(Fn); - for (auto ORI : llvm::enumerate(SyclOptReport.GetInfo(FD))) { - llvm::DiagnosticLocation DL = - SourceLocToDebugLoc(ORI.value().KernelArgLoc); - StringRef NameInDesc = ORI.value().KernelArgDescName; - StringRef ArgType = ORI.value().KernelArgType; - StringRef ArgDesc = ORI.value().KernelArgDesc; - unsigned ArgSize = ORI.value().KernelArgSize; - StringRef ArgDecomposedField = ORI.value().KernelArgDecomposedField; - - llvm::OptimizationRemark Remark("sycl", "Region", DL, - &Fn->getEntryBlock()); - Remark << "Arg " << llvm::ore::NV("Argument", ORI.index()) << ":" - << ArgDesc << NameInDesc << " (" << ArgDecomposedField - << "Type:" << ArgType << ", " - << "Size: " << llvm::ore::NV("Argument", ArgSize) << ")"; - ORE.emit(Remark); + if (!getLangOpts().OptRecordFile.empty()) { + SyclOptReportHandler &SyclOptReport = CGM.getDiags().getSYCLOptReport(); + if (Fn && SyclOptReport.HasOptReportInfo(FD)) { + llvm::OptimizationRemarkEmitter ORE(Fn); + for (auto ORI : llvm::enumerate(SyclOptReport.GetInfo(FD))) { + llvm::DiagnosticLocation DL = + SourceLocToDebugLoc(ORI.value().KernelArgLoc); + StringRef NameInDesc = ORI.value().KernelArgDescName; + StringRef ArgType = ORI.value().KernelArgType; + StringRef ArgDesc = ORI.value().KernelArgDesc; + unsigned ArgSize = ORI.value().KernelArgSize; + StringRef ArgDecomposedField = ORI.value().KernelArgDecomposedField; + + llvm::OptimizationRemark Remark("sycl", "Region", DL, + &Fn->getEntryBlock()); + Remark << "Arg " << llvm::ore::NV("Argument", ORI.index()) << ":" + << ArgDesc << NameInDesc << " (" << ArgDecomposedField + << "Type:" << ArgType << ", " + << "Size: " << llvm::ore::NV("Argument", ArgSize) << ")"; + ORE.emit(Remark); + } } } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 80e321dc5a4cf..ca154d17145fe 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1946,6 +1946,8 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, bool NeedLocTracking = false; + Opts.OptRecordFile = LangOpts->OptRecordFile; + if (!Opts.OptRecordFile.empty()) NeedLocTracking = true; @@ -3318,6 +3320,8 @@ void CompilerInvocation::GenerateLangArgs(const LangOptions &Opts, GenerateArg(Args, OPT_pic_is_pie, SA); for (StringRef Sanitizer : serializeSanitizerKinds(Opts.Sanitize)) GenerateArg(Args, OPT_fsanitize_EQ, Sanitizer, SA); + if (!Opts.OptRecordFile.empty()) + GenerateArg(Args, OPT_opt_record_file, Opts.OptRecordFile, SA); return; } @@ -3617,6 +3621,12 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ), Diags, Opts.Sanitize); + // OptRecordFile is used to generate the optimization record file should + // be set regardless of the input type. + if (Args.hasArg(OPT_opt_record_file)) + Opts.OptRecordFile = + std::string(Args.getLastArgValue(OPT_opt_record_file)); + return Diags.getNumErrors() == NumErrorsBefore; } diff --git a/clang/lib/Sema/SemaSYCL.cpp b/clang/lib/Sema/SemaSYCL.cpp index 17538d32e47a4..98f90d8c602da 100644 --- a/clang/lib/Sema/SemaSYCL.cpp +++ b/clang/lib/Sema/SemaSYCL.cpp @@ -3669,17 +3669,29 @@ void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc, SyclOptReportCreator opt_report(*this, kernel_decl, KernelObj->getLocation()); KernelObjVisitor Visitor{*this}; - Visitor.VisitRecordBases(KernelObj, kernel_decl, kernel_body, int_header, - int_footer, opt_report); - Visitor.VisitRecordFields(KernelObj, kernel_decl, kernel_body, int_header, - int_footer, opt_report); + + // Visit handlers to generate information for optimization record only if + // optimization record is saved. + if (!getLangOpts().OptRecordFile.empty()) { + Visitor.VisitRecordBases(KernelObj, kernel_decl, kernel_body, int_header, + int_footer, opt_report); + Visitor.VisitRecordFields(KernelObj, kernel_decl, kernel_body, int_header, + int_footer, opt_report); + } else { + Visitor.VisitRecordBases(KernelObj, kernel_decl, kernel_body, int_header, + int_footer); + Visitor.VisitRecordFields(KernelObj, kernel_decl, kernel_body, int_header, + int_footer); + } if (ParmVarDecl *KernelHandlerArg = getSyclKernelHandlerArg(KernelCallerFunc)) { kernel_decl.handleSyclKernelHandlerType(); kernel_body.handleSyclKernelHandlerType(KernelHandlerArg); int_header.handleSyclKernelHandlerType(KernelHandlerArg->getType()); - opt_report.handleSyclKernelHandlerType(); + + if (!getLangOpts().OptRecordFile.empty()) + opt_report.handleSyclKernelHandlerType(); } }