10
10
11
11
namespace Slang
12
12
{
13
- // Only attempt to precompile functions:
13
+ // Only attempt to precompile functions and global variables :
14
14
// 1) With function bodies (not just empty decls)
15
15
// 2) Not marked with unsafeForceInlineDecoration
16
16
// 3) Have a simple HLSL data type as the return or parameter type
17
17
static bool attemptPrecompiledExport (IRInst* inst)
18
18
{
19
- if (inst->getOp () != kIROp_Func )
19
+ if (inst->getOp () != kIROp_Func && inst-> getOp () != kIROp_GlobalVar )
20
20
{
21
21
return false ;
22
22
}
23
23
24
- // Skip functions with no body
25
- bool hasBody = false ;
26
- for (auto child : inst->getChildren ())
24
+ if (inst->getOp () == kIROp_GlobalVar )
27
25
{
28
- if (child->getOp () == kIROp_Block )
26
+ inst->dump ();
27
+ if (inst->findDecoration <IRExportDecoration>())
29
28
{
30
- hasBody = true ;
31
- break ;
29
+ return true ;
32
30
}
33
- }
34
- if (!hasBody)
35
- {
36
31
return false ;
37
32
}
38
-
39
- // Skip functions marked with unsafeForceInlineDecoration
40
- if (inst->findDecoration <IRUnsafeForceInlineEarlyDecoration>())
33
+ else if (inst->getOp () == kIROp_Func )
41
34
{
42
- return false ;
35
+ // Skip functions with no body
36
+ bool hasBody = false ;
37
+ for (auto child : inst->getChildren ())
38
+ {
39
+ if (child->getOp () == kIROp_Block )
40
+ {
41
+ hasBody = true ;
42
+ break ;
43
+ }
44
+ }
45
+ if (!hasBody)
46
+ {
47
+ return false ;
48
+ }
49
+
50
+ // Skip functions marked with unsafeForceInlineDecoration
51
+ if (inst->findDecoration <IRUnsafeForceInlineEarlyDecoration>())
52
+ {
53
+ return false ;
54
+ }
43
55
}
44
56
45
57
// Skip non-simple HLSL data types, filters out generics
@@ -51,6 +63,18 @@ static bool attemptPrecompiledExport(IRInst* inst)
51
63
return true ;
52
64
}
53
65
66
+ static bool needsImport (IRInst* inst)
67
+ {
68
+ if (inst->getOp () == kIROp_GlobalVar )
69
+ {
70
+ if (inst->findDecoration <IRUserExternDecoration>())
71
+ {
72
+ return true ;
73
+ }
74
+ }
75
+ return false ;
76
+ }
77
+
54
78
/*
55
79
* Precompile the module for the given target.
56
80
*
@@ -160,24 +184,31 @@ Module::precompileForTarget(SlangCompileTarget target, slang::IBlob** outDiagnos
160
184
// the linked result to see which functions survived the pruning and are included in the
161
185
// precompiled blob.
162
186
Dictionary<String, IRInst*> nameToFunction;
163
- bool hasAtLeastOneFunction = false ;
187
+ bool hasAtLeastOneExport = false ;
164
188
for (auto inst : module->getGlobalInsts ())
165
189
{
166
190
if (attemptPrecompiledExport (inst))
167
191
{
168
- hasAtLeastOneFunction = true ;
192
+ hasAtLeastOneExport = true ;
169
193
builder.addDecoration (inst, kIROp_DownstreamModuleExportDecoration );
170
194
nameToFunction[inst->findDecoration <IRExportDecoration>()->getMangledName ()] = inst;
171
195
}
196
+ if (needsImport (inst))
197
+ {
198
+ builder.addDecoration (inst, kIROp_DownstreamModuleImportDecoration );
199
+ }
172
200
}
173
201
174
- // Bail if there are no functions to export. That's not treated as an error
175
- // because it's possible that the module just doesn't have any simple HLSL .
176
- if (!hasAtLeastOneFunction )
202
+ // Bail if there is nothing to export. That's not treated as an error
203
+ // because it's possible that the module just doesn't have any simple code .
204
+ if (!hasAtLeastOneExport )
177
205
{
178
206
return SLANG_OK;
179
207
}
180
208
209
+ // dump module
210
+ module->getModuleInst ()->dump ();
211
+
181
212
ComPtr<IArtifact> outArtifact;
182
213
SlangResult res = codeGenContext.emitPrecompiledDownstreamIR (outArtifact);
183
214
@@ -208,7 +239,7 @@ Module::precompileForTarget(SlangCompileTarget target, slang::IBlob** outDiagnos
208
239
// represent functions that were pruned from the IR after linking, before target generation.
209
240
for (auto moduleInst : module->getGlobalInsts ())
210
241
{
211
- if (moduleInst->getOp () == kIROp_Func )
242
+ // if (moduleInst->getOp() == kIROp_Func)
212
243
{
213
244
if (auto dec = moduleInst->findDecoration <IRDownstreamModuleExportDecoration>())
214
245
{
0 commit comments