3131#include " llvm/IR/DiagnosticInfo.h"
3232#include " llvm/IR/DiagnosticPrinter.h"
3333#include " llvm/Support/raw_ostream.h"
34- #include " llvm/ADT/STLExtras.h"
3534#include " llvm/ADT/SetVector.h"
3635#include < unordered_set>
3736
37+ using std::make_unique;
38+
3839using namespace llvm ;
3940using std::string;
4041using std::vector;
@@ -85,18 +86,8 @@ const char *kDxIsHelperGlobalName = "dx.ishelper";
8586const char *kHostLayoutTypePrefix = " hostlayout." ;
8687}
8788
88- // Avoid dependency on DxilModule from llvm::Module using this:
89- void DxilModule_RemoveGlobal (llvm::Module* M, llvm::GlobalObject* G) {
90- if (M && G && M->HasDxilModule ()) {
91- if (llvm::Function *F = dyn_cast<llvm::Function>(G))
92- M->GetDxilModule ().RemoveFunction (F);
93- }
94- }
95- void DxilModule_ResetModule (llvm::Module* M) {
96- if (M && M->HasDxilModule ())
97- delete &M->GetDxilModule ();
98- M->SetDxilModule (nullptr );
99- }
89+ void SetDxilHook (Module &M);
90+ void ClearDxilHook (Module &M);
10091
10192// ------------------------------------------------------------------------------
10293//
@@ -109,16 +100,16 @@ DxilModule::DxilModule(Module *pModule)
109100, m_pModule(pModule)
110101, m_pEntryFunc(nullptr )
111102, m_EntryName(" " )
112- , m_pMDHelper(llvm:: make_unique<DxilMDHelper>(pModule, llvm:: make_unique<DxilExtraPropertyHelper>(pModule)))
103+ , m_pMDHelper(make_unique<DxilMDHelper>(pModule, make_unique<DxilExtraPropertyHelper>(pModule)))
113104, m_pDebugInfoFinder(nullptr )
114105, m_pSM(nullptr )
115106, m_DxilMajor(DXIL::kDxilMajor )
116107, m_DxilMinor(DXIL::kDxilMinor )
117108, m_ValMajor(1 )
118109, m_ValMinor(0 )
119110, m_ForceZeroStoreLifetimes(false )
120- , m_pOP(llvm:: make_unique<OP>(pModule->getContext (), pModule))
121- , m_pTypeSystem(llvm:: make_unique<DxilTypeSystem>(pModule))
111+ , m_pOP(make_unique<OP>(pModule->getContext (), pModule))
112+ , m_pTypeSystem(make_unique<DxilTypeSystem>(pModule))
122113, m_bDisableOptimizations(false )
123114, m_bUseMinPrecision(true ) // use min precision by default
124115, m_bAllResourcesBound(false )
@@ -129,8 +120,7 @@ DxilModule::DxilModule(Module *pModule)
129120{
130121
131122 DXASSERT_NOMSG (m_pModule != nullptr );
132- m_pModule->pfnRemoveGlobal = &DxilModule_RemoveGlobal;
133- m_pModule->pfnResetDxilModule = &DxilModule_ResetModule;
123+ SetDxilHook (*m_pModule);
134124
135125#if defined(_DEBUG) || defined(DBG)
136126 // Pin LLVM dump methods.
@@ -142,10 +132,7 @@ DxilModule::DxilModule(Module *pModule)
142132#endif
143133}
144134
145- DxilModule::~DxilModule () {
146- if (m_pModule->pfnRemoveGlobal == &DxilModule_RemoveGlobal)
147- m_pModule->pfnRemoveGlobal = nullptr ;
148- }
135+ DxilModule::~DxilModule () { ClearDxilHook (*m_pModule); }
149136
150137LLVMContext &DxilModule::GetCtx () const { return m_Ctx; }
151138Module *DxilModule::GetModule () const { return m_pModule; }
@@ -167,7 +154,7 @@ void DxilModule::SetShaderModel(const ShaderModel *pSM, bool bUseMinPrecision) {
167154 DxilFunctionProps props;
168155 props.shaderKind = m_pSM->GetKind ();
169156 m_DxilEntryPropsMap[nullptr ] =
170- llvm:: make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
157+ make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
171158 }
172159 m_SerializedRootSignature.clear ();
173160}
@@ -1209,7 +1196,7 @@ void DxilModule::ReplaceDxilEntryProps(llvm::Function *F,
12091196void DxilModule::CloneDxilEntryProps (llvm::Function *F, llvm::Function *NewF) {
12101197 DXASSERT (m_DxilEntryPropsMap.count (F) != 0 , " cannot find F in map" );
12111198 std::unique_ptr<DxilEntryProps> Props =
1212- llvm:: make_unique<DxilEntryProps>(*m_DxilEntryPropsMap[F]);
1199+ make_unique<DxilEntryProps>(*m_DxilEntryPropsMap[F]);
12131200 m_DxilEntryPropsMap[NewF] = std::move (Props);
12141201}
12151202
@@ -1497,7 +1484,7 @@ void DxilModule::EmitDxilMetadata() {
14971484 MDTuple *pSig = m_pMDHelper->EmitDxilSignatures (entryProps->sig );
14981485
14991486 MDTuple *pSubEntry = m_pMDHelper->EmitDxilEntryPointTuple (
1500- const_cast <Function *>(F), F->getName (), pSig, nullptr , pProps);
1487+ const_cast <Function *>(F), F->getName (). str () , pSig, nullptr , pProps);
15011488
15021489 Entries.emplace_back (pSubEntry);
15031490 }
@@ -1589,7 +1576,7 @@ void DxilModule::LoadDxilMetadata() {
15891576 }
15901577
15911578 std::unique_ptr<DxilEntryProps> pEntryProps =
1592- llvm:: make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
1579+ make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
15931580 m_pMDHelper->LoadDxilSignatures (*pSignatures, pEntryProps->sig );
15941581
15951582 m_DxilEntryPropsMap[pFunc] = std::move (pEntryProps);
@@ -1603,7 +1590,7 @@ void DxilModule::LoadDxilMetadata() {
16031590 }
16041591 } else {
16051592 std::unique_ptr<DxilEntryProps> pEntryProps =
1606- llvm:: make_unique<DxilEntryProps>(entryFuncProps, m_bUseMinPrecision);
1593+ make_unique<DxilEntryProps>(entryFuncProps, m_bUseMinPrecision);
16071594 DxilFunctionProps *pFuncProps = &pEntryProps->props ;
16081595 m_pMDHelper->LoadDxilSignatures (*pEntrySignatures, pEntryProps->sig );
16091596
@@ -1722,6 +1709,10 @@ StripResourcesReflection(std::vector<std::unique_ptr<TResource>> &vec) {
17221709 return bChanged;
17231710}
17241711
1712+ bool isSequentialType (Type *Ty) {
1713+ return isa<ArrayType>(Ty) || isa<VectorType>(Ty) || isa<PointerType>(Ty);
1714+ }
1715+
17251716// Return true if any members or components of struct <Ty> contain
17261717// scalars of less than 32 bits or are matrices, in which case translation is required
17271718typedef llvm::SmallSetVector<const StructType*, 4 > SmallStructSetVector;
@@ -1732,9 +1723,8 @@ static bool ResourceTypeRequiresTranslation(const StructType * Ty, SmallStructSe
17321723 containedStructs.insert (Ty);
17331724 for (auto eTy : Ty->elements ()) {
17341725 // Skip past all levels of sequential types to test their elements
1735- SequentialType *seqTy;
1736- while ((seqTy = dyn_cast<SequentialType>(eTy))) {
1737- eTy = seqTy->getElementType ();
1726+ while ((isSequentialType (eTy))) {
1727+ eTy = eTy->getContainedType (0 );
17381728 }
17391729 // Recursively call this function again to process internal structs
17401730 if (StructType *structTy = dyn_cast<StructType>(eTy)) {
@@ -1954,39 +1944,12 @@ void DxilModule::StripDebugRelatedCode() {
19541944}
19551945DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder () {
19561946 if (m_pDebugInfoFinder == nullptr ) {
1957- m_pDebugInfoFinder = llvm:: make_unique<llvm::DebugInfoFinder>();
1947+ m_pDebugInfoFinder = make_unique<llvm::DebugInfoFinder>();
19581948 m_pDebugInfoFinder->processModule (*m_pModule);
19591949 }
19601950 return *m_pDebugInfoFinder;
19611951}
19621952
1963- hlsl::DxilModule *hlsl::DxilModule::TryGetDxilModule (llvm::Module *pModule) {
1964- LLVMContext &Ctx = pModule->getContext ();
1965- std::string diagStr;
1966- raw_string_ostream diagStream (diagStr);
1967-
1968- hlsl::DxilModule *pDxilModule = nullptr ;
1969- // TODO: add detail error in DxilMDHelper.
1970- try {
1971- pDxilModule = &pModule->GetOrCreateDxilModule ();
1972- } catch (const ::hlsl::Exception &hlslException) {
1973- diagStream << " load dxil metadata failed -" ;
1974- try {
1975- const char *msg = hlslException.what ();
1976- if (msg == nullptr || *msg == ' \0 ' )
1977- diagStream << " error code " << hlslException.hr << " \n " ;
1978- else
1979- diagStream << msg;
1980- } catch (...) {
1981- diagStream << " unable to retrieve error message.\n " ;
1982- }
1983- Ctx.diagnose (DxilErrorDiagnosticInfo (diagStream.str ().c_str ()));
1984- } catch (...) {
1985- Ctx.diagnose (DxilErrorDiagnosticInfo (" load dxil metadata failed - unknown error.\n " ));
1986- }
1987- return pDxilModule;
1988- }
1989-
19901953// Check if the instruction has fast math flags configured to indicate
19911954// the instruction is precise.
19921955// Precise fast math flags means none of the fast math flags are set.
@@ -2028,18 +1991,3 @@ bool DxilModule::IsPrecise(const Instruction *inst) const {
20281991}
20291992
20301993} // namespace hlsl
2031-
2032- namespace llvm {
2033- hlsl::DxilModule &Module::GetOrCreateDxilModule (bool skipInit) {
2034- std::unique_ptr<hlsl::DxilModule> M;
2035- if (!HasDxilModule ()) {
2036- M = llvm::make_unique<hlsl::DxilModule>(this );
2037- if (!skipInit) {
2038- M->LoadDxilMetadata ();
2039- }
2040- SetDxilModule (M.release ());
2041- }
2042- return GetDxilModule ();
2043- }
2044-
2045- }
0 commit comments