Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/dxc/DXIL/DxilModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class DxilModule {
void ResetEntryPropsMap(DxilEntryPropsMap &&PropMap);

bool StripReflection();
bool StripNamesSensitiveToDebug();
void StripDebugRelatedCode();
void RemoveUnusedTypeAnnotations();

Expand Down
2 changes: 2 additions & 0 deletions include/dxc/HLSL/DxilGenerationPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ FunctionPass *createMatrixBitcastLowerPass();
ModulePass *createDxilCleanupAddrSpaceCastPass();
ModulePass *createDxilRenameResourcesPass();
ModulePass *createDxilScalarizeVectorIntrinsicsPass();
ModulePass *createDxilStripDebugSensitiveInfoPass();

void initializeDxilLowerCreateHandleForLibPass(llvm::PassRegistry &);
void initializeDxilAllocateResourcesForLibPass(llvm::PassRegistry &);
Expand Down Expand Up @@ -117,6 +118,7 @@ void initializeMatrixBitcastLowerPassPass(llvm::PassRegistry &);
void initializeDxilCleanupAddrSpaceCastPass(llvm::PassRegistry &);
void initializeDxilRenameResourcesPass(llvm::PassRegistry &);
void initializeDxilScalarizeVectorIntrinsicsPass(llvm::PassRegistry &);
void initializeDxilStripDebugSensitiveInfoPass(llvm::PassRegistry &);

ModulePass *createDxilValidateWaveSensitivityPass();
void initializeDxilValidateWaveSensitivityPass(llvm::PassRegistry &);
Expand Down
5 changes: 4 additions & 1 deletion include/dxc/HLSL/DxilLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DxilLinker {
void SetValidatorVersion(unsigned valMajor, unsigned valMinor) {
m_valMajor = valMajor, m_valMinor = valMinor;
}
void SetStripDebug(bool StripDebug) { m_StripDebug = StripDebug; }
virtual bool HasLibNameRegistered(llvm::StringRef name) = 0;
virtual bool RegisterLib(llvm::StringRef name,
std::unique_ptr<llvm::Module> pModule,
Expand All @@ -56,9 +57,11 @@ class DxilLinker {

protected:
DxilLinker(llvm::LLVMContext &Ctx, unsigned valMajor, unsigned valMinor)
: m_ctx(Ctx), m_valMajor(valMajor), m_valMinor(valMinor) {}
: m_ctx(Ctx), m_valMajor(valMajor), m_valMinor(valMinor),
m_StripDebug(false) {}
llvm::LLVMContext &m_ctx;
unsigned m_valMajor, m_valMinor;
bool m_StripDebug;
};

} // namespace hlsl
1 change: 1 addition & 0 deletions include/llvm/Transforms/IPO/PassManagerBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class PassManagerBuilder {
bool HLSLEnableDebugNops = false; // HLSL Change
bool HLSLEarlyInlining = true; // HLSL Change
bool HLSLNoSink = false; // HLSL Change
bool StripDebug = false;
void addHLSLPasses(legacy::PassManagerBase &MPM); // HLSL Change

private:
Expand Down
51 changes: 51 additions & 0 deletions lib/DXIL/DxilModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/Support/raw_ostream.h"
#include <unordered_set>

Expand Down Expand Up @@ -1826,6 +1827,56 @@ bool DxilModule::StripReflection() {
return bChanged;
}

bool DxilModule::StripNamesSensitiveToDebug() {
bool changed = false;

if (!GetShaderModel()->IsLib()) {
// Strip struct names
unsigned nextStructId = 0;
TypeFinder StructTypes;
StructTypes.run(*m_pModule, true);
for (StructType *STy : StructTypes) {
if (!STy->hasName())
continue;

StringRef Name = STy->getName();
if (Name.startswith("dx."))
continue;

STy->setName((Twine("dx.strip.struct.") + Twine(nextStructId++)).str());
changed = true;
}

// Strip entry function name
if (m_pEntryFunc) {
SetEntryFunctionName("dx.strip.entry.");
m_pEntryFunc->setName("dx.strip.entry.");
changed = true;
}

// Strip groupshared variable names
unsigned nextGroupSharedId = 0;
for (GlobalVariable &globalVar : m_pModule->globals()) {
if (globalVar.getType()->getPointerAddressSpace() ==
DXIL::kTGSMAddrSpace &&
globalVar.hasName()) {
StringRef Name = globalVar.getName();
if (Name.startswith("dx.") || Name.startswith("llvm."))
continue;
globalVar.setName(
(Twine("dx.strip.tgsm.") + Twine(nextGroupSharedId++)).str());
changed = true;
}
}

// ReEmit meta.
if (changed)
ReEmitDxilResources();
}

return changed;
}

static void RemoveTypesFromSet(Type *Ty,
SetVector<const StructType *> &typeSet) {
if (Ty->isPointerTy())
Expand Down
1 change: 1 addition & 0 deletions lib/HLSL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_llvm_library(LLVMHLSL
DxilRenameResourcesPass.cpp
DxilScalarizeVectorIntrinsics.cpp
DxilSimpleGVNHoist.cpp
DxilStripDebugSensitiveInfo.cpp
DxilSignatureValidation.cpp
DxilTargetLowering.cpp
DxilTargetTransformInfo.cpp
Expand Down
10 changes: 7 additions & 3 deletions lib/HLSL/DxilLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ namespace {
// Create module from link defines.
struct DxilLinkJob {
DxilLinkJob(LLVMContext &Ctx, dxilutil::ExportMap &exportMap,
unsigned valMajor, unsigned valMinor)
unsigned valMajor, unsigned valMinor, bool StripDebug)
: m_ctx(Ctx), m_exportMap(exportMap), m_valMajor(valMajor),
m_valMinor(valMinor) {}
m_valMinor(valMinor), m_StripDebug(StripDebug) {}
std::unique_ptr<llvm::Module>
Link(std::pair<DxilFunctionLinkInfo *, DxilLib *> &entryLinkPair,
const ShaderModel *pSM);
Expand Down Expand Up @@ -394,6 +394,7 @@ struct DxilLinkJob {
LLVMContext &m_ctx;
dxilutil::ExportMap &m_exportMap;
unsigned m_valMajor, m_valMinor;
bool m_StripDebug;
};
} // namespace

Expand Down Expand Up @@ -1296,6 +1297,9 @@ void DxilLinkJob::RunPreparePass(Module &M) {
PM.add(createDxilEmitMetadataPass());
PM.add(createDxilFinalizePreservesPass());

if (m_StripDebug)
PM.add(createDxilStripDebugSensitiveInfoPass());

PM.run(M);
}

Expand Down Expand Up @@ -1488,7 +1492,7 @@ DxilLinkerImpl::Link(StringRef entry, StringRef profile,
return nullptr;
}

DxilLinkJob linkJob(m_ctx, exportMap, m_valMajor, m_valMinor);
DxilLinkJob linkJob(m_ctx, exportMap, m_valMajor, m_valMinor, m_StripDebug);

SetVector<DxilLib *> libSet;
SetVector<StringRef> addedFunctionSet;
Expand Down
47 changes: 47 additions & 0 deletions lib/HLSL/DxilStripDebugSensitiveInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
///////////////////////////////////////////////////////////////////////////////
// //
// DxilStripDebugSensitiveInfo.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Pass to strip debug-sensitive information from DXIL modules. //
// //
///////////////////////////////////////////////////////////////////////////////

#include "dxc/DXIL/DxilModule.h"
#include "dxc/HLSL/DxilGenerationPass.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"

using namespace llvm;
using namespace hlsl;

namespace {

class DxilStripDebugSensitiveInfo : public ModulePass {
public:
static char ID;
explicit DxilStripDebugSensitiveInfo() : ModulePass(ID) {}

StringRef getPassName() const override {
return "DXIL Strip Debug-Sensitive Information";
}

bool runOnModule(Module &M) override {
if (!M.HasDxilModule())
return false;
return M.GetOrCreateDxilModule().StripNamesSensitiveToDebug();
}
};

char DxilStripDebugSensitiveInfo::ID = 0;

} // namespace

ModulePass *llvm::createDxilStripDebugSensitiveInfoPass() {
return new DxilStripDebugSensitiveInfo();
}

INITIALIZE_PASS(DxilStripDebugSensitiveInfo, "hlsl-dxil-strip-debug-info",
"HLSL DXIL Strip Debug-Sensitive Information", false, false)
5 changes: 5 additions & 0 deletions lib/Transforms/IPO/PassManagerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ PassManagerBuilder::PassManagerBuilder() {
VerifyOutput = false;
MergeFunctions = false;
PrepareForLTO = false;
StripDebug = false;
}

PassManagerBuilder::~PassManagerBuilder() {
Expand Down Expand Up @@ -393,6 +394,8 @@ void PassManagerBuilder::populateModulePassManager(
MPM.add(createDxilDeleteRedundantDebugValuesPass());
MPM.add(createNoPausePassesPass());
MPM.add(createDxilEmitMetadataPass());
if (StripDebug)
MPM.add(createDxilStripDebugSensitiveInfoPass());
}
// HLSL Change Ends.
return;
Expand Down Expand Up @@ -712,6 +715,8 @@ void PassManagerBuilder::populateModulePassManager(
MPM.add(createNoPausePassesPass());
MPM.add(createDxilValidateWaveSensitivityPass());
MPM.add(createDxilEmitMetadataPass());
if (StripDebug)
MPM.add(createDxilStripDebugSensitiveInfoPass());
}
// HLSL Change Ends.
addExtensionsToPM(EP_OptimizerLast, MPM);
Expand Down
2 changes: 2 additions & 0 deletions tools/clang/include/clang/Frontend/CodeGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// Name of the profile file to use as input for -fprofile-instr-use
std::string InstrProfileInput;

bool StripDebug = false;

/// A list of file names passed with -fcuda-include-gpubinary options to
/// forward to CUDA runtime back-end for incorporating them into host-side
/// object file.
Expand Down
1 change: 1 addition & 0 deletions tools/clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ void EmitAssemblyHelper::CreatePasses() {
OptToggles.IsEnabled(hlsl::options::TOGGLE_PARTIAL_LIFETIME_MARKERS);
PMBuilder.HLSLEnableAggressiveReassociation = OptToggles.IsEnabled(
hlsl::options::TOGGLE_ENABLE_AGGRESSIVE_REASSOCIATION);
PMBuilder.StripDebug = CodeGenOpts.StripDebug;
// HLSL Change - end

PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
Expand Down
2 changes: 2 additions & 0 deletions tools/clang/tools/dxcompiler/dxclinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ HRESULT STDMETHODCALLTYPE DxcLinker::Link(
dxcutil::GetValidatorVersion(&valMajor, &valMinor);
}
m_pLinker->SetValidatorVersion(valMajor, valMinor);
m_pLinker->SetStripDebug(opts.StripDebug);

// Root signature-only container validation is only supported on 1.5 and
// above.
Expand Down Expand Up @@ -415,6 +416,7 @@ HRESULT STDMETHODCALLTYPE DxcLinker::Link(
SerializeFlags, pOutputStream, 0, opts.DebugFile, &Diag,
&ShaderHashContent, pReflectionStream, pRootSigStream, nullptr,
nullptr);

if (needsValidation) {
valHR = dxcutil::ValidateAndAssembleToContainer(inputs);
} else {
Expand Down
1 change: 1 addition & 0 deletions tools/clang/tools/dxcompiler/dxcompilerobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,7 @@ class DxcCompiler : public IDxcCompiler3,
Opts.EnableLifetimeMarkers;
compiler.getCodeGenOpts().HLSLEnablePayloadAccessQualifiers =
Opts.EnablePayloadQualifiers;
compiler.getCodeGenOpts().StripDebug = Opts.StripDebug;

// Translate signature packing options
if (Opts.PackPrefixStable)
Expand Down
Loading