Skip to content

Commit 8b8956f

Browse files
Fix error on shutdown when using custom memory allocator (microsoft#4656)
ShaderModel::Get() used a static std::unordered_map which is torn down when custom allocator may no longer be active.
1 parent c033915 commit 8b8956f

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/DXIL/DxilShaderModel.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "dxc/DXIL/DxilShaderModel.h"
1313
#include "dxc/DXIL/DxilSemantic.h"
1414
#include "dxc/Support/Global.h"
15-
#include <unordered_map>
15+
#include <algorithm>
1616

1717

1818
namespace hlsl {
@@ -83,7 +83,7 @@ bool ShaderModel::IsValidForModule() const {
8383
const ShaderModel *ShaderModel::Get(Kind Kind, unsigned Major, unsigned Minor) {
8484
/* <py::lines('VALRULE-TEXT')>hctdb_instrhelp.get_shader_model_get()</py>*/
8585
// VALRULE-TEXT:BEGIN
86-
const static std::unordered_map<unsigned, unsigned> hashToIdxMap = {
86+
const static std::pair<unsigned, unsigned> hashToIdxMap[] = {
8787
{1024,0}, //ps_4_0
8888
{1025,1}, //ps_4_1
8989
{1280,2}, //ps_5_0
@@ -169,8 +169,9 @@ const ShaderModel *ShaderModel::Get(Kind Kind, unsigned Major, unsigned Minor) {
169169
{919047,81}, //as_6_7
170170
};
171171
unsigned hash = (unsigned)Kind << 16 | Major << 8 | Minor;
172-
auto it = hashToIdxMap.find(hash);
173-
if (it == hashToIdxMap.end())
172+
auto pred = [](const std::pair<unsigned, unsigned>& elem, unsigned val){ return elem.first < val;};
173+
auto it = std::lower_bound(std::begin(hashToIdxMap), std::end(hashToIdxMap), hash, pred);
174+
if (it == std::end(hashToIdxMap))
174175
return GetInvalid();
175176
return &ms_ShaderModels[it->second];
176177
// VALRULE-TEXT:END

utils/hct/hctdb_instrhelp.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ def get_num_shader_models():
12931293

12941294
def build_shader_model_hash_idx_map():
12951295
#must match get_shader_models.
1296-
result = "const static std::unordered_map<unsigned, unsigned> hashToIdxMap = {\n"
1296+
result = "const static std::pair<unsigned, unsigned> hashToIdxMap[] = {\n"
12971297
count = 0
12981298
for profile in shader_profiles:
12991299
min_sm = profile.start_sm
@@ -1386,11 +1386,12 @@ def get_dxil_version():
13861386
return result
13871387

13881388
def get_shader_model_get():
1389-
# const static std::unordered_map<unsigned, unsigned> hashToIdxMap = {};
1389+
# const static std::pair<unsigned, unsigned> hashToIdxMap[] = {};
13901390
result = build_shader_model_hash_idx_map()
13911391
result += "unsigned hash = (unsigned)Kind << 16 | Major << 8 | Minor;\n"
1392-
result += "auto it = hashToIdxMap.find(hash);\n"
1393-
result += "if (it == hashToIdxMap.end())\n"
1392+
result += "auto pred = [](const std::pair<unsigned, unsigned>& elem, unsigned val){ return elem.first < val;};\n"
1393+
result += "auto it = std::lower_bound(std::begin(hashToIdxMap), std::end(hashToIdxMap), hash, pred);\n"
1394+
result += "if (it == std::end(hashToIdxMap))\n"
13941395
result += " return GetInvalid();\n"
13951396
result += "return &ms_ShaderModels[it->second];"
13961397
return result

0 commit comments

Comments
 (0)