@@ -129,25 +129,6 @@ static LegalType legalizeType(
129
129
return legalizeType (context->typeLegalizationContext , type);
130
130
}
131
131
132
- // Legalize a type, and then expect it to
133
- // result in a simple type.
134
- static IRType* legalizeSimpleType (
135
- IRTypeLegalizationContext* context,
136
- IRType* type)
137
- {
138
- auto legalType = legalizeType (context, type);
139
- switch (legalType.flavor )
140
- {
141
- case LegalType::Flavor::simple:
142
- return legalType.getSimple ();
143
-
144
- default :
145
- // TODO: need to issue a diagnostic here.
146
- SLANG_UNEXPECTED (" unexpected type case" );
147
- break ;
148
- }
149
- }
150
-
151
132
// Take a value that is being used as an operand,
152
133
// and turn it into the equivalent legalized value.
153
134
static LegalVal legalizeOperand (
@@ -209,21 +190,47 @@ static LegalVal legalizeCall(
209
190
IRTypeLegalizationContext* context,
210
191
IRCall* callInst)
211
192
{
212
- // TODO: implement legalization of non-simple return types
213
193
auto retType = legalizeType (context, callInst->getFullType ());
214
- SLANG_ASSERT (retType.flavor == LegalType::Flavor::simple);
194
+ IRType* retIRType = nullptr ;
195
+ switch (retType.flavor )
196
+ {
197
+ case LegalType::Flavor::simple:
198
+ retIRType = retType.getSimple ();
199
+ break ;
200
+ case LegalType::Flavor::none:
201
+ retIRType = context->builder ->getVoidType ();
202
+ break ;
203
+ default :
204
+ // TODO: implement legalization of non-simple return types
205
+ SLANG_UNEXPECTED (" unimplemented legalized return type for IRInstCall." );
206
+ }
215
207
216
208
List<IRInst*> instArgs;
217
209
for (auto i = 1u ; i < callInst->getOperandCount (); i++)
218
210
getArgumentValues (instArgs, legalizeOperand (context, callInst->getOperand (i)));
219
211
220
212
return LegalVal::simple (context->builder ->emitCallInst (
221
- callInst-> getFullType () ,
213
+ retIRType ,
222
214
callInst->getCallee (),
223
215
instArgs.Count (),
224
216
instArgs.Buffer ()));
225
217
}
226
218
219
+ static LegalVal legalizeRetVal (IRTypeLegalizationContext* context,
220
+ LegalVal retVal)
221
+ {
222
+ switch (retVal.flavor )
223
+ {
224
+ case LegalVal::Flavor::simple:
225
+ return LegalVal::simple (context->builder ->emitReturn (retVal.getSimple ()));
226
+ case LegalVal::Flavor::none:
227
+ return LegalVal::simple (context->builder ->emitReturn ());
228
+ default :
229
+ // TODO: implement legalization of non-simple return types
230
+ SLANG_UNEXPECTED (" unimplemented legalized return type for IRReturnVal." );
231
+ }
232
+ }
233
+
227
234
static LegalVal legalizeLoad (
228
235
IRTypeLegalizationContext* context,
229
236
LegalVal legalPtrVal)
@@ -343,6 +350,9 @@ static LegalVal legalizeFieldExtract(
343
350
{
344
351
auto builder = context->builder ;
345
352
353
+ if (type.flavor == LegalType::Flavor::none)
354
+ return LegalVal ();
355
+
346
356
switch (legalStructOperand.flavor )
347
357
{
348
358
case LegalVal::Flavor::none:
@@ -459,6 +469,8 @@ static LegalVal legalizeFieldAddress(
459
469
IRStructKey* fieldKey)
460
470
{
461
471
auto builder = context->builder ;
472
+ if (type.flavor == LegalType::Flavor::none)
473
+ return LegalVal ();
462
474
463
475
switch (legalPtrOperand.flavor )
464
476
{
@@ -940,7 +952,20 @@ static LegalVal legalizeMakeStruct(
940
952
}
941
953
}
942
954
943
-
955
+ static LegalVal legalizeConstruct (IRTypeLegalizationContext* context,
956
+ LegalType type)
957
+ {
958
+ switch (type.flavor )
959
+ {
960
+ case LegalType::Flavor::none:
961
+ return LegalVal ();
962
+ case LegalType::Flavor::simple:
963
+ return LegalVal::simple (context->builder ->emitConstructorInst (type.getSimple (), 0 , nullptr ));
964
+ default :
965
+ SLANG_UNEXPECTED (" unhandled legalization case for construct inst." );
966
+ UNREACHABLE_RETURN (LegalVal ());
967
+ }
968
+ }
944
969
945
970
static LegalVal legalizeInst (
946
971
IRTypeLegalizationContext* context,
@@ -970,14 +995,16 @@ static LegalVal legalizeInst(
970
995
971
996
case kIROp_Call :
972
997
return legalizeCall (context, (IRCall*)inst);
973
-
998
+ case kIROp_ReturnVal :
999
+ return legalizeRetVal (context, args[0 ]);
974
1000
case kIROp_makeStruct :
975
1001
return legalizeMakeStruct (
976
1002
context,
977
1003
type,
978
1004
args,
979
1005
inst->getOperandCount ());
980
-
1006
+ case kIROp_Construct :
1007
+ return legalizeConstruct (context, type);
981
1008
case kIROp_undefined :
982
1009
return LegalVal ();
983
1010
default :
@@ -1186,10 +1213,10 @@ static LegalVal legalizeInst(
1186
1213
// will, in general, depend on what we are doing.
1187
1214
1188
1215
// We will set up the IR builder so that any new
1189
- // instructions generated will be placed after
1216
+ // instructions generated will be placed before
1190
1217
// the location of the original instruction.
1191
1218
auto builder = context->builder ;
1192
- builder->setInsertBefore (inst-> getNextInst () );
1219
+ builder->setInsertBefore (inst);
1193
1220
1194
1221
LegalVal legalVal = legalizeInst (
1195
1222
context,
@@ -1279,7 +1306,19 @@ static LegalVal legalizeFunc(
1279
1306
// TODO: we should give an error message when the result type of a function
1280
1307
// can't be legalized (e.g., trying to return a texture, or a structue that
1281
1308
// contains one).
1282
- IRType* newResultType = legalizeSimpleType (context, oldFuncType->getResultType ());
1309
+ auto legalReturnType = legalizeType (context, oldFuncType->getResultType ());
1310
+ IRType* newResultType = nullptr ;
1311
+ switch (legalReturnType.flavor )
1312
+ {
1313
+ case LegalType::Flavor::simple:
1314
+ newResultType = legalReturnType.getSimple ();
1315
+ break ;
1316
+ case LegalType::Flavor::none:
1317
+ newResultType = context->builder ->getVoidType ();
1318
+ break ;
1319
+ default :
1320
+ SLANG_UNEXPECTED (" unknown legalized function return type." );
1321
+ }
1283
1322
List<IRType*> newParamTypes;
1284
1323
for (UInt pp = 0 ; pp < oldParamCount; ++pp)
1285
1324
{
@@ -1295,7 +1334,6 @@ static LegalVal legalizeFunc(
1295
1334
context->builder ->setDataType (irFunc, newFuncType);
1296
1335
1297
1336
legalizeInstsInParent (context, irFunc);
1298
-
1299
1337
return LegalVal::simple (irFunc);
1300
1338
}
1301
1339
@@ -1360,10 +1398,9 @@ static LegalVal declareSimpleVar(
1360
1398
1361
1399
case kIROp_Var :
1362
1400
{
1401
+ builder->setInsertBefore (context->insertBeforeLocalVar );
1363
1402
auto localVar = builder->emitVar (type);
1364
- localVar->removeFromParent ();
1365
- localVar->insertBefore (context->insertBeforeLocalVar );
1366
-
1403
+
1367
1404
irVar = localVar;
1368
1405
legalVarVal = LegalVal::simple (irVar);
1369
1406
0 commit comments