Skip to content

Commit

Permalink
Fix SPIRV val issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe committed Mar 4, 2025
1 parent 3192259 commit 520addf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion external/spirv-tools
42 changes: 13 additions & 29 deletions source/slang/slang-emit-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1439,8 +1439,6 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex

bool shouldEmitArrayStride(IRInst* elementType)
{
if (isResourceType((IRType*)elementType))
return false;
for (auto decor : elementType->getDecorations())
{
switch (decor->getOp())
Expand Down Expand Up @@ -1670,41 +1668,22 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
case kIROp_ArrayType:
case kIROp_UnsizedArrayType:
{
const auto elementType = static_cast<IRArrayTypeBase*>(inst)->getElementType();
auto irArrayType = static_cast<IRArrayTypeBase*>(inst);
const auto elementType = irArrayType->getElementType();
const auto arrayType =
inst->getOp() == kIROp_ArrayType
? emitOpTypeArray(
inst,
elementType,
static_cast<IRArrayTypeBase*>(inst)->getElementCount())
? emitOpTypeArray(inst, elementType, irArrayType->getElementCount())
: emitOpTypeRuntimeArray(inst, elementType);
auto strideInst = as<IRArrayTypeBase>(inst)->getArrayStride();
int stride = 0;
if (strideInst)
{
stride = (int)getIntVal(strideInst);
}
else
{
IRSizeAndAlignment sizeAndAlignment;
getNaturalSizeAndAlignment(
m_targetProgram->getOptionSet(),
elementType,
&sizeAndAlignment);
stride = (int)sizeAndAlignment.getStride();
}

// Avoid validation error: Arrays whose element type is a Block or BufferBlock,
// or an opaque resource must not be decorated with ArrayStride.
if (shouldEmitArrayStride(elementType))
auto strideInst = irArrayType->getArrayStride();
if (strideInst && shouldEmitArrayStride(irArrayType->getElementType()))
{
int stride = (int)getIntVal(strideInst);
emitOpDecorateArrayStride(
getSection(SpvLogicalSectionID::Annotations),
nullptr,
arrayType,
SpvLiteralInteger::from32(stride));
}

return arrayType;
}
case kIROp_AtomicType:
Expand Down Expand Up @@ -4989,6 +4968,7 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
layoutRuleName = layout->getLayoutName();
}
int32_t id = 0;
bool isPhysicalType = structType->findDecoration<IRPhysicalTypeDecoration>();
for (auto field : structType->getFields())
{
for (auto decor : field->getKey()->getDecorations())
Expand Down Expand Up @@ -5069,6 +5049,10 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
}
}

if (!isPhysicalType)
continue;

// Emit explicit struct field layout decorations if the struct is physical.
IRIntegerValue offset = 0;
if (auto offsetDecor = field->getKey()->findDecoration<IRPackOffsetDecoration>())
{
Expand Down Expand Up @@ -5099,8 +5083,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
if (matrixType)
{
// SPIRV sepc on MatrixStride:
// Applies only to a member of a structure type.Only valid on a
// matrix or array whose most basic element is a matrix.Matrix
// Applies only to a member of a structure type. Only valid on a
// matrix or array whose most basic element is a matrix. Matrix
// Stride is an unsigned 32 - bit integer specifying the stride
// of the rows in a RowMajor - decorated matrix or columns in a
// ColMajor - decorated matrix.
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-ir-inst-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ INST_RANGE(BindingQuery, GetRegisterIndex, GetRegisterSpace)
INST(InterpolationModeDecoration, interpolationMode, 1, 0)
INST(NameHintDecoration, nameHint, 1, 0)

INST(PhysicalTypeDecoration, PhysicalType, 1, 0)

// Marks a type as being used as binary interface (e.g. shader parameters).
// This prevents the legalizeEmptyType() pass from eliminating it on C++/CUDA targets.
INST(BinaryInterfaceTypeDecoration, BinaryInterfaceType, 0, 0)
Expand Down
6 changes: 6 additions & 0 deletions source/slang/slang-ir-insts.h
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ IR_SIMPLE_DECORATION(ForceInlineDecoration)

IR_SIMPLE_DECORATION(ForceUnrollDecoration)

IR_SIMPLE_DECORATION(PhysicalTypeDecoration)

struct IRSizeAndAlignmentDecoration : IRDecoration
{
IR_LEAF_ISA(SizeAndAlignmentDecoration)
Expand Down Expand Up @@ -4725,6 +4727,10 @@ struct IRBuilder
IRVarLayout* getVarLayout(List<IRInst*> const& operands);
IREntryPointLayout* getEntryPointLayout(IRVarLayout* paramsLayout, IRVarLayout* resultLayout);

void addPhysicalTypeDecoration(IRInst* value)
{
addDecoration(value, kIROp_PhysicalTypeDecoration);
}

void addNameHintDecoration(IRInst* value, IRStringLit* name)
{
Expand Down
8 changes: 7 additions & 1 deletion source/slang/slang-ir-lower-buffer-element-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ struct LoweredElementTypeContext
}

auto loweredType = builder.createStructType();
builder.addPhysicalTypeDecoration(loweredType);

StringBuilder nameSB;
bool isColMajor =
getIntVal(matrixType->getLayout()) == SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
Expand Down Expand Up @@ -551,7 +553,7 @@ struct LoweredElementTypeContext
// For spirv backend, we always want to lower all array types for non-varying
// parameters, even if the element type comes out the same. This is because different
// layout rules may have different array stride requirements.
if (!target->shouldEmitSPIRVDirectly() || config.addressSpace == AddressSpace::Input)
if (!target->shouldEmitSPIRVDirectly())
{
if (!loweredInnerTypeInfo.convertLoweredToOriginal)
{
Expand All @@ -561,6 +563,8 @@ struct LoweredElementTypeContext
}

auto loweredType = builder.createStructType();
builder.addPhysicalTypeDecoration(loweredType);

info.loweredType = loweredType;
StringBuilder nameSB;
nameSB << "_Array_" << getLayoutName(config.layoutRule->ruleName) << "_";
Expand Down Expand Up @@ -625,6 +629,8 @@ struct LoweredElementTypeContext
}
}
auto loweredType = builder.createStructType();
builder.addPhysicalTypeDecoration(loweredType);

StringBuilder nameSB;
getTypeNameHint(nameSB, type);
nameSB << "_" << getLayoutName(config.layoutRule->ruleName);
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-ir-spirv-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct SPIRVLegalizationContext : public SourceEmitterBase
inst->getElementType(),
builder.getIntValue(builder.getIntType(), elementSize.getStride()));
const auto structType = builder.createStructType();
builder.addPhysicalTypeDecoration(structType);
const auto arrayKey = builder.createStructKey();
builder.createStructField(structType, arrayKey, arrayType);
IRSizeAndAlignment structSize;
Expand Down Expand Up @@ -213,6 +214,7 @@ struct SPIRVLegalizationContext : public SourceEmitterBase
IRBuilder builder(cbParamInst);
builder.setInsertBefore(cbParamInst);
auto structType = builder.createStructType();
builder.addPhysicalTypeDecoration(structType);
addToWorkList(structType);
StringBuilder sb;
sb << "cbuffer_";
Expand Down

0 comments on commit 520addf

Please sign in to comment.