Index: clang/include/clang/Basic/CodeGenOptions.h =================================================================== --- clang/include/clang/Basic/CodeGenOptions.h +++ clang/include/clang/Basic/CodeGenOptions.h @@ -171,6 +171,10 @@ /// The ABI to use for passing floating point arguments. std::string FloatABI; + /// The file to use for dumping bug report by `VerifyDIPreserve` for original + /// debug info. + std::string DIBugsReportFilePath; + /// The floating-point denormal mode to use. llvm::DenormalMode FPDenormalMode = llvm::DenormalMode::getIEEE(); Index: clang/include/clang/Basic/CodeGenOptions.def =================================================================== --- clang/include/clang/Basic/CodeGenOptions.def +++ clang/include/clang/Basic/CodeGenOptions.def @@ -67,6 +67,10 @@ CODEGENOPT(DisableRedZone , 1, 0) ///< Set when -mno-red-zone is enabled. CODEGENOPT(EmitCallSiteInfo, 1, 0) ///< Emit call site info only in the case of ///< '-g' + 'O>0' level. +CODEGENOPT(EnableDIPreservationVerify, 1, 0) ///< Enable di preservation verify + ///< each (it means check + ///< the original debug info + ///< metadata preservation). CODEGENOPT(IndirectTlsSegRefs, 1, 0) ///< Set when -mno-tls-direct-seg-refs ///< is specified. CODEGENOPT(DisableTailCalls , 1, 0) ///< Do not emit tail calls. Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -3894,6 +3894,16 @@ def fexperimental_debug_variable_locations : Flag<["-"], "fexperimental-debug-variable-locations">, HelpText<"Use experimental new value-tracking variable locations">; +def fverify_debuginfo_preserve + : Flag<["-"], "fverify-debuginfo-preserve">, + HelpText<"Enable Debug Info Metadata preservation testing in " + "optimizations.">; +def fverify_debuginfo_preserve_export + : Joined<["-"], "fverify-debuginfo-preserve-export=">, + MetaVarName<"">, + HelpText<"Export debug info (by testing original Debug Info) failures " + "into specified (JSON) file (should be abs path as we use " + "append mode to insert new JSON objects).">; // The driver option takes the key as a parameter to the -msign-return-address= // and -mbranch-protection= options, but CC1 has a separate option so we // don't have to parse the parameter twice. Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -82,6 +82,7 @@ #include "llvm/Transforms/Utils/NameAnonGlobals.h" #include "llvm/Transforms/Utils/SymbolRewriter.h" #include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h" +#include "llvm/Transforms/Utils/VerifyDIPreserve.h" #include using namespace clang; using namespace llvm; @@ -893,7 +894,17 @@ if (TM) TheModule->setDataLayout(TM->createDataLayout()); - legacy::PassManager PerModulePasses; + VerifyDIPreserveCustomPassManager PerModulePasses; + DebugInfoPerPassMap DIPreservationMap; + if (CodeGenOpts.EnableDIPreservationVerify) { + PerModulePasses.setVerifyDIPreserveMode( + VerifyDIPreserveMode::OriginalDebugInfo); + PerModulePasses.setDIPreservationMap(DIPreservationMap); + + if (!CodeGenOpts.DIBugsReportFilePath.empty()) + PerModulePasses.setOrigDIVerifyBugsReportFilePath( + CodeGenOpts.DIBugsReportFilePath); + } PerModulePasses.add( createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -835,6 +835,16 @@ llvm::is_contained(DebugEntryValueArchs, T.getArch())) Opts.EmitCallSiteInfo = true; + Opts.EnableDIPreservationVerify = + Args.hasArg(OPT_fverify_debuginfo_preserve); + // Ignore the option if the -fverify-debuginfo-preserve wasn't enabled. + if (Opts.EnableDIPreservationVerify && + Args.hasArg(OPT_fverify_debuginfo_preserve_export)) { + Opts.DIBugsReportFilePath = + std::string( + Args.getLastArgValue(OPT_fverify_debuginfo_preserve_export)); + } + Opts.ValueTrackingVariableLocations = Args.hasArg(OPT_fexperimental_debug_variable_locations); Index: clang/test/Driver/verify-debug-info-preservation.c =================================================================== --- /dev/null +++ clang/test/Driver/verify-debug-info-preservation.c @@ -0,0 +1,14 @@ +// We support the CC1 options for testing whether each LLVM pass preserves +// original debug info. + +// RUN: %clang -g -Xclang -fverify-debuginfo-preserve -### %s 2>&1 \ +// RUN: | FileCheck --check-prefix=VERIFYDIPRESERVE %s + +// VERIFYDIPRESERVE: "-fverify-debuginfo-preserve" + +// RUN: %clang -g -Xclang -fverify-debuginfo-preserve \ +// RUN: -Xclang -fverify-debuginfo-preserve-export=%t.json -### %s 2>&1 \ +// RUN: | FileCheck --check-prefix=VERIFYDIPRESERVE-JSON-EXPORT %s + +// VERIFYDIPRESERVE-JSON-EXPORT: "-fverify-debuginfo-preserve" +// VERIFYDIPRESERVE-JSON-EXPORT: "-fverify-debuginfo-preserve-export={{.*}}" Index: llvm/docs/HowToUpdateDebugInfo.rst =================================================================== --- llvm/docs/HowToUpdateDebugInfo.rst +++ llvm/docs/HowToUpdateDebugInfo.rst @@ -317,6 +317,16 @@ $ llvm-original-di-preservation.py sample.json sample.html +The `original-di-check` mode can be invoked from front-end level as follows: + +.. code-block:: bash + + # Test each pass. + $ clang -Xclang -fverify-debuginfo-preserve -g -O2 sample.c + + # Test each pass and export the issues report into the JSON file. + $ clang -Xclang -fverify-debuginfo-preserve -Xclang -fverify-debuginfo-preserve-export=sample.json -g -O2 sample.c + Using ``VerifyDIPreserve`` ^^^^^^^^^^^^^^^^^^^^^^^^^^