Skip to content

Commit 2fc210f

Browse files
committed
Revert "Fix nullptr in generic specialization" and add emitPtrLit instead
1 parent 191e795 commit 2fc210f

File tree

3 files changed

+95
-83
lines changed

3 files changed

+95
-83
lines changed

source/slang/slang-ir-clone.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,27 @@ IRInst* cloneInstAndOperands(IRCloneEnv* env, IRBuilder* builder, IRInst* oldIns
5151
SLANG_ASSERT(builder);
5252
SLANG_ASSERT(oldInst);
5353

54-
// IRConstants contain data other than just the operands, so cloning them
55-
// is done separately.
56-
IRConstant* oldConstant = as<IRConstant>(oldInst);
57-
if (oldConstant)
54+
// Pointer literals need to be handled separately, as they carry other data
55+
// than just the operands.
56+
if (oldInst->getOp() == kIROp_PtrLit)
5857
{
59-
IRConstant* newConstant = builder->getClonedConstantValue(*oldConstant);
60-
builder->addInst(newConstant);
61-
newConstant->sourceLoc = oldInst->sourceLoc;
62-
return newConstant;
58+
auto oldPtr = as<IRPtrLit>(oldInst);
59+
auto newInst = builder->emitPtrLit(oldPtr->getFullType(), oldPtr->value.ptrVal);
60+
newInst->sourceLoc = oldPtr->sourceLoc;
61+
return newInst;
6362
}
6463

64+
// This logic will not handle any instructions
65+
// with special-case data attached, but that only
66+
// applies to `IRConstant`s at this point, and those
67+
// should only appear at the global scope rather than
68+
// in function bodies.
69+
//
70+
// TODO: It would be easy enough to extend this logic
71+
// to handle constants gracefully, if it ever comes up.
72+
//
73+
SLANG_ASSERT(!as<IRConstant>(oldInst));
74+
6575
// We start by mapping the type of the orignal instruction
6676
// to its replacement value, if any.
6777
//

source/slang/slang-ir-insts.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -3601,7 +3601,6 @@ struct IRBuilder
36013601
IRPtrLit* getNullVoidPtrValue() { return getNullPtrValue(getPtrType(getVoidType())); }
36023602
IRVoidLit* getVoidValue();
36033603
IRInst* getCapabilityValue(CapabilitySet const& caps);
3604-
IRConstant* getClonedConstantValue(IRConstant& value);
36053604

36063605
IRBasicType* getBasicType(BaseType baseType);
36073606
IRBasicType* getVoidType();
@@ -4434,6 +4433,8 @@ struct IRBuilder
44344433

44354434
IRGlobalConstant* emitGlobalConstant(IRType* type, IRInst* val);
44364435

4436+
IRConstant* emitPtrLit(IRType* type, void* ptr);
4437+
44374438
IRInst* emitWaveMaskBallot(IRType* type, IRInst* mask, IRInst* condition);
44384439
IRInst* emitWaveMaskMatch(IRType* type, IRInst* mask, IRInst* value);
44394440

source/slang/slang-ir.cpp

+75-74
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,71 @@ IRConstant* IRBuilder::_findOrEmitConstant(IRConstant& keyInst)
21922192
return irValue;
21932193
}
21942194

2195-
irValue = getClonedConstantValue(keyInst);
2195+
// Calculate the minimum object size (ie not including the payload of value)
2196+
const size_t prefixSize = SLANG_OFFSET_OF(IRConstant, value);
2197+
2198+
switch (keyInst.getOp())
2199+
{
2200+
default:
2201+
SLANG_UNEXPECTED("missing case for IR constant");
2202+
break;
2203+
2204+
case kIROp_BoolLit:
2205+
case kIROp_IntLit:
2206+
{
2207+
const size_t instSize = prefixSize + sizeof(IRIntegerValue);
2208+
irValue = static_cast<IRConstant*>(
2209+
_createInst(instSize, keyInst.getFullType(), keyInst.getOp()));
2210+
irValue->value.intVal = keyInst.value.intVal;
2211+
break;
2212+
}
2213+
case kIROp_FloatLit:
2214+
{
2215+
const size_t instSize = prefixSize + sizeof(IRFloatingPointValue);
2216+
irValue = static_cast<IRConstant*>(
2217+
_createInst(instSize, keyInst.getFullType(), keyInst.getOp()));
2218+
irValue->value.floatVal = keyInst.value.floatVal;
2219+
break;
2220+
}
2221+
case kIROp_PtrLit:
2222+
{
2223+
const size_t instSize = prefixSize + sizeof(void*);
2224+
irValue = static_cast<IRConstant*>(
2225+
_createInst(instSize, keyInst.getFullType(), keyInst.getOp()));
2226+
irValue->value.ptrVal = keyInst.value.ptrVal;
2227+
break;
2228+
}
2229+
case kIROp_VoidLit:
2230+
{
2231+
const size_t instSize = prefixSize + sizeof(void*);
2232+
irValue = static_cast<IRConstant*>(
2233+
_createInst(instSize, keyInst.getFullType(), keyInst.getOp()));
2234+
irValue->value.ptrVal = keyInst.value.ptrVal;
2235+
break;
2236+
}
2237+
case kIROp_BlobLit:
2238+
case kIROp_StringLit:
2239+
{
2240+
const UnownedStringSlice slice = keyInst.getStringSlice();
2241+
2242+
const size_t sliceSize = slice.getLength();
2243+
const size_t instSize =
2244+
prefixSize + offsetof(IRConstant::StringValue, chars) + sliceSize;
2245+
2246+
irValue = static_cast<IRConstant*>(
2247+
_createInst(instSize, keyInst.getFullType(), keyInst.getOp()));
2248+
2249+
IRConstant::StringValue& dstString = irValue->value.stringVal;
2250+
2251+
dstString.numChars = uint32_t(sliceSize);
2252+
// Turn into pointer to avoid warning of array overrun
2253+
char* dstChars = dstString.chars;
2254+
// Copy the chars
2255+
memcpy(dstChars, slice.begin(), sliceSize);
2256+
2257+
break;
2258+
}
2259+
}
21962260

21972261
key.inst = irValue;
21982262
m_dedupContext->getConstantMap().add(key, irValue);
@@ -2405,79 +2469,6 @@ IRInst* IRBuilder::getCapabilityValue(CapabilitySet const& caps)
24052469
conjunctions.getBuffer());
24062470
}
24072471

2408-
IRConstant* IRBuilder::getClonedConstantValue(IRConstant& value)
2409-
{
2410-
IRConstant* irValue = nullptr;
2411-
2412-
// Calculate the minimum object size (ie not including the payload of value)
2413-
const size_t prefixSize = SLANG_OFFSET_OF(IRConstant, value);
2414-
2415-
switch (value.getOp())
2416-
{
2417-
default:
2418-
SLANG_UNEXPECTED("missing case for IR constant");
2419-
break;
2420-
2421-
case kIROp_BoolLit:
2422-
case kIROp_IntLit:
2423-
{
2424-
const size_t instSize = prefixSize + sizeof(IRIntegerValue);
2425-
irValue =
2426-
static_cast<IRConstant*>(_createInst(instSize, value.getFullType(), value.getOp()));
2427-
irValue->value.intVal = value.value.intVal;
2428-
break;
2429-
}
2430-
case kIROp_FloatLit:
2431-
{
2432-
const size_t instSize = prefixSize + sizeof(IRFloatingPointValue);
2433-
irValue =
2434-
static_cast<IRConstant*>(_createInst(instSize, value.getFullType(), value.getOp()));
2435-
irValue->value.floatVal = value.value.floatVal;
2436-
break;
2437-
}
2438-
case kIROp_PtrLit:
2439-
{
2440-
const size_t instSize = prefixSize + sizeof(void*);
2441-
irValue =
2442-
static_cast<IRConstant*>(_createInst(instSize, value.getFullType(), value.getOp()));
2443-
irValue->value.ptrVal = value.value.ptrVal;
2444-
break;
2445-
}
2446-
case kIROp_VoidLit:
2447-
{
2448-
const size_t instSize = prefixSize + sizeof(void*);
2449-
irValue =
2450-
static_cast<IRConstant*>(_createInst(instSize, value.getFullType(), value.getOp()));
2451-
irValue->value.ptrVal = value.value.ptrVal;
2452-
break;
2453-
}
2454-
case kIROp_BlobLit:
2455-
case kIROp_StringLit:
2456-
{
2457-
const UnownedStringSlice slice = value.getStringSlice();
2458-
2459-
const size_t sliceSize = slice.getLength();
2460-
const size_t instSize =
2461-
prefixSize + offsetof(IRConstant::StringValue, chars) + sliceSize;
2462-
2463-
irValue =
2464-
static_cast<IRConstant*>(_createInst(instSize, value.getFullType(), value.getOp()));
2465-
2466-
IRConstant::StringValue& dstString = irValue->value.stringVal;
2467-
2468-
dstString.numChars = uint32_t(sliceSize);
2469-
// Turn into pointer to avoid warning of array overrun
2470-
char* dstChars = dstString.chars;
2471-
// Copy the chars
2472-
memcpy(dstChars, slice.begin(), sliceSize);
2473-
2474-
break;
2475-
}
2476-
}
2477-
2478-
return irValue;
2479-
}
2480-
24812472
static void canonicalizeInstOperands(IRBuilder& builder, IROp op, ArrayView<IRInst*> operands)
24822473
{
24832474
// For Array types, we always want to make sure its element count
@@ -5853,6 +5844,16 @@ IRGlobalConstant* IRBuilder::emitGlobalConstant(IRType* type, IRInst* val)
58535844
return inst;
58545845
}
58555846

5847+
IRConstant* IRBuilder::emitPtrLit(IRType* type, void* ptrVal)
5848+
{
5849+
const size_t prefixSize = SLANG_OFFSET_OF(IRConstant, value);
5850+
const size_t instSize = prefixSize + sizeof(void*);
5851+
auto inst = static_cast<IRConstant*>(_createInst(instSize, type, kIROp_PtrLit));
5852+
inst->value.ptrVal = ptrVal;
5853+
addInst(inst);
5854+
return inst;
5855+
}
5856+
58565857
IRInst* IRBuilder::emitWaveMaskBallot(IRType* type, IRInst* mask, IRInst* condition)
58575858
{
58585859
auto inst = createInst<IRInst>(this, kIROp_WaveMaskBallot, type, mask, condition);

0 commit comments

Comments
 (0)