Skip to content

Commit

Permalink
Fix nullptr in generic specialization (#6066)
Browse files Browse the repository at this point in the history
* Fix nullptr in generic specialization

* Fix formatting

* Revert "Fix nullptr in generic specialization" and add emitPtrLit instead

* Add type parameter to getPtrValue()

---------

Co-authored-by: Yong He <[email protected]>
  • Loading branch information
juliusikkala and csyonghe authored Jan 17, 2025
1 parent 9143087 commit ddc4a17
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
20 changes: 14 additions & 6 deletions source/slang/slang-ir-clone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ IRInst* cloneInstAndOperands(IRCloneEnv* env, IRBuilder* builder, IRInst* oldIns
SLANG_ASSERT(builder);
SLANG_ASSERT(oldInst);

// We start by mapping the type of the orignal instruction
// to its replacement value, if any.
//
auto oldType = oldInst->getFullType();
auto newType = (IRType*)findCloneForOperand(env, oldType);

// Pointer literals need to be handled separately, as they carry other data
// than just the operands.
if (oldInst->getOp() == kIROp_PtrLit)
{
auto oldPtr = as<IRPtrLit>(oldInst);
return builder->getPtrValue(newType, oldPtr->value.ptrVal);
}

// This logic will not handle any instructions
// with special-case data attached, but that only
// applies to `IRConstant`s at this point, and those
Expand All @@ -62,12 +76,6 @@ IRInst* cloneInstAndOperands(IRCloneEnv* env, IRBuilder* builder, IRInst* oldIns
//
SLANG_ASSERT(!as<IRConstant>(oldInst));

// We start by mapping the type of the orignal instruction
// to its replacement value, if any.
//
auto oldType = oldInst->getFullType();
auto newType = (IRType*)findCloneForOperand(env, oldType);

// Next we will iterate over the operands of `oldInst`
// to find their replacements and install them as
// the operands of `newInst`.
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-ir-insts.h
Original file line number Diff line number Diff line change
Expand Up @@ -3599,7 +3599,7 @@ struct IRBuilder
IRInst* getFloatValue(IRType* type, IRFloatingPointValue value);
IRStringLit* getStringValue(const UnownedStringSlice& slice);
IRBlobLit* getBlobValue(ISlangBlob* blob);
IRPtrLit* _getPtrValue(void* ptr);
IRPtrLit* getPtrValue(IRType* type, void* ptr);
IRPtrLit* getNullPtrValue(IRType* type);
IRPtrLit* getNullVoidPtrValue() { return getNullPtrValue(getPtrType(getVoidType())); }
IRVoidLit* getVoidValue();
Expand Down
9 changes: 9 additions & 0 deletions source/slang/slang-ir-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,15 @@ IRInst* cloneInst(
{
// We need to special-case any instruction that is not
// allocated like an ordinary `IRInst` with trailing args.
case kIROp_IntLit:
case kIROp_FloatLit:
case kIROp_BoolLit:
case kIROp_StringLit:
case kIROp_BlobLit:
case kIROp_PtrLit:
case kIROp_VoidLit:
return cloneValue(context, originalInst);

case kIROp_Func:
return cloneFuncImpl(context, builder, cast<IRFunc>(originalInst), originalValues);

Expand Down
5 changes: 2 additions & 3 deletions source/slang/slang-ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2395,9 +2395,8 @@ IRBlobLit* IRBuilder::getBlobValue(ISlangBlob* blob)
return static_cast<IRBlobLit*>(_findOrEmitConstant(keyInst));
}

IRPtrLit* IRBuilder::_getPtrValue(void* data)
IRPtrLit* IRBuilder::getPtrValue(IRType* type, void* data)
{
auto type = getPtrType(getVoidType());
IRConstant keyInst;
memset(&keyInst, 0, sizeof(keyInst));
keyInst.m_op = kIROp_PtrLit;
Expand Down Expand Up @@ -6324,7 +6323,7 @@ IRDecoration* IRBuilder::addDecoration(

void IRBuilder::addHighLevelDeclDecoration(IRInst* inst, Decl* decl)
{
auto ptrConst = _getPtrValue(decl);
auto ptrConst = getPtrValue(getPtrType(getVoidType()), decl);
addDecoration(inst, kIROp_HighLevelDeclDecoration, ptrConst);
}

Expand Down

0 comments on commit ddc4a17

Please sign in to comment.