Skip to content

Commit 736c2aa

Browse files
authored
Code cleanup to help build with newer version of llvm. (#4080)
* Code cleanup to help build with newer version of llvm. * Use c++14 to enable std::make_unique for linux build. * Merge llvm/llvm-project@e9110d7#diff-302b958882f0490f56723d598234f0cc7d3e4d3f3693e6850defdba6b75596af to fix MacOS build error for random_shuffle is marked deprecated.
1 parent 9f0b253 commit 736c2aa

9 files changed

Lines changed: 513 additions & 373 deletions

File tree

cmake/modules/HandleLLVMOptions.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,17 @@ elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
432432
check_cxx_compiler_flag("-std=c++1y" CXX_SUPPORTS_CXX1Y)
433433
append_if(CXX_SUPPORTS_CXX1Y "-std=c++1y" CMAKE_CXX_FLAGS)
434434
else()
435-
check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11)
436-
if (CXX_SUPPORTS_CXX11)
435+
check_cxx_compiler_flag("-std=c++14" CXX_SUPPORTS_CXX14)
436+
if (CXX_SUPPORTS_CXX14)
437437
if (CYGWIN OR MINGW)
438438
# MinGW and Cygwin are a bit stricter and lack things like
439439
# 'strdup', 'stricmp', etc in c++11 mode.
440-
append("-std=gnu++11" CMAKE_CXX_FLAGS)
440+
append("-std=gnu++14" CMAKE_CXX_FLAGS)
441441
else()
442-
append("-std=c++11" CMAKE_CXX_FLAGS)
442+
append("-std=c++14" CMAKE_CXX_FLAGS)
443443
endif()
444444
else()
445-
message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++11' flag isn't supported.")
445+
message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++14' flag isn't supported.")
446446
endif()
447447
endif()
448448
if (LLVM_ENABLE_MODULES)

lib/DXIL/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_llvm_library(LLVMDXIL
1313
DxilInterpolationMode.cpp
1414
DxilMetadataHelper.cpp
1515
DxilModule.cpp
16+
DxilModuleHelper.cpp
1617
DxilOperations.cpp
1718
DxilResource.cpp
1819
DxilResourceBase.cpp
@@ -27,6 +28,7 @@ add_llvm_library(LLVMDXIL
2728
DxilSubobject.cpp
2829
DxilTypeSystem.cpp
2930
DxilUtil.cpp
31+
DxilUtilDbgInfoAndMisc.cpp
3032
DxilPDB.cpp
3133

3234
ADDITIONAL_HEADER_DIRS

lib/DXIL/DxilMetadataHelper.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void DxilMDHelper::LoadDxilShaderModel(const ShaderModel *&pSM) {
264264
IFTBOOL(pShaderTypeMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
265265
unsigned Major = ConstMDToUint32(pShaderModelMD->getOperand(kDxilShaderModelMajorIdx));
266266
unsigned Minor = ConstMDToUint32(pShaderModelMD->getOperand(kDxilShaderModelMinorIdx));
267-
string ShaderModelName = pShaderTypeMD->getString();
267+
string ShaderModelName = pShaderTypeMD->getString().str();
268268
ShaderModelName += "_" + std::to_string(Major) + "_" +
269269
(Minor == ShaderModel::kOfflineMinor ? "x" : std::to_string(Minor));
270270
pSM = ShaderModel::GetByName(ShaderModelName.c_str());
@@ -384,7 +384,7 @@ void DxilMDHelper::GetDxilEntryPoint(const MDNode *MDO, Function *&pFunc, string
384384
IFTBOOL(MDOName.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
385385
MDString *pMDName = dyn_cast<MDString>(MDOName);
386386
IFTBOOL(pMDName != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
387-
Name = pMDName->getString();
387+
Name = pMDName->getString().str();
388388

389389
pSignatures = &pTupleMD->getOperand(kDxilEntryPointSignatures);
390390
pResources = &pTupleMD->getOperand(kDxilEntryPointResources );
@@ -2580,20 +2580,14 @@ bool DxilMDHelper::IsKnownMetadataID(LLVMContext &Ctx, unsigned ID)
25802580

25812581
void DxilMDHelper::GetKnownMetadataIDs(LLVMContext &Ctx, SmallVectorImpl<unsigned> *pIDs)
25822582
{
2583-
auto AddIdIfExists = [&Ctx, &pIDs](StringRef Name) {
2584-
unsigned ID = 0;
2585-
if (Ctx.findMDKindID(hlsl::DxilMDHelper::kDxilPreciseAttributeMDName,
2586-
&ID)) {
2587-
pIDs->push_back(ID);
2583+
SmallVector<StringRef, 4> Names;
2584+
Ctx.getMDKindNames(Names);
2585+
for (auto Name : Names) {
2586+
if (Name == hlsl::DxilMDHelper::kDxilPreciseAttributeMDName ||
2587+
Name == hlsl::DxilMDHelper::kDxilNonUniformAttributeMDName) {
2588+
pIDs->push_back(Ctx.getMDKindID(Name));
25882589
}
2589-
if (Ctx.findMDKindID(hlsl::DxilMDHelper::kDxilNonUniformAttributeMDName,
2590-
&ID)) {
2591-
pIDs->push_back(ID);
2592-
}
2593-
};
2594-
2595-
AddIdIfExists(hlsl::DxilMDHelper::kDxilPreciseAttributeMDName);
2596-
AddIdIfExists(hlsl::DxilMDHelper::kDxilNonUniformAttributeMDName);
2590+
}
25972591
}
25982592

25992593
void DxilMDHelper::combineDxilMetadata(llvm::Instruction *K,
@@ -2689,7 +2683,7 @@ float DxilMDHelper::ConstMDToFloat(const MDOperand &MDO) {
26892683
string DxilMDHelper::StringMDToString(const MDOperand &MDO) {
26902684
MDString *pMDString = dyn_cast<MDString>(MDO.get());
26912685
IFTBOOL(pMDString != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
2692-
return pMDString->getString();
2686+
return pMDString->getString().str();
26932687
}
26942688

26952689
StringRef DxilMDHelper::StringMDToStringRef(const MDOperand &MDO) {

lib/DXIL/DxilModule.cpp

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
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+
3839
using namespace llvm;
3940
using std::string;
4041
using std::vector;
@@ -85,18 +86,8 @@ const char *kDxIsHelperGlobalName = "dx.ishelper";
8586
const 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

150137
LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
151138
Module *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,
12091196
void 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
17271718
typedef 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
}
19551945
DebugInfoFinder &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

Comments
 (0)