Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AnyValue marshalling for 8-bit integers #6059

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions source/slang/slang-ir-any-value-marshalling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,25 @@ struct AnyValueMarshallingContext
}
case kIROp_Int8Type:
case kIROp_UInt8Type:
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcVal = builder->emitLoad(concreteVar);
srcVal = builder->emitCast(builder->getType(kIROp_UIntType), srcVal);
auto dstAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
auto dstVal = builder->emitLoad(dstAddr);
dstVal = builder->emitBitfieldInsert(
dstVal->getFullType(),
dstVal,
srcVal,
builder->getIntValue(builder->getUIntType(), 8 * intraFieldOffset),
builder->getIntValue(builder->getUIntType(), 8));
builder->emitStore(dstAddr, dstVal);
}
advanceOffset(1);
break;
case kIROp_UInt64Type:
case kIROp_Int64Type:
case kIROp_DoubleType:
Expand Down Expand Up @@ -600,11 +619,35 @@ struct AnyValueMarshallingContext
advanceOffset(2);
break;
}
case kIROp_Int8Type:
case kIROp_UInt8Type:
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
auto srcVal = builder->emitLoad(srcAddr);
srcVal = builder->emitBitfieldExtract(
srcVal->getFullType(),
srcVal,
builder->getIntValue(builder->getUIntType(), 8 * intraFieldOffset),
builder->getIntValue(builder->getUIntType(), 8));
if (dataType->getOp() == kIROp_Int8Type)
{
srcVal = builder->emitCast(builder->getType(kIROp_Int8Type), srcVal);
}
else
{
srcVal = builder->emitCast(builder->getType(kIROp_UInt8Type), srcVal);
}
builder->emitStore(concreteVar, srcVal);
}
advanceOffset(1);
break;
case kIROp_UInt64Type:
case kIROp_Int64Type:
case kIROp_DoubleType:
case kIROp_Int8Type:
case kIROp_UInt8Type:
case kIROp_PtrType:
#if SLANG_PTR_IS_64
case kIROp_IntPtrType:
Expand Down Expand Up @@ -832,7 +875,7 @@ SlangInt _getAnyValueSizeRaw(IRType* type, SlangInt offset)
return alignUp(offset, 2) + 2;
case kIROp_UInt8Type:
case kIROp_Int8Type:
return -1;
return offset + 1;
case kIROp_VectorType:
{
auto vectorType = static_cast<IRVectorType*>(type);
Expand Down
68 changes: 68 additions & 0 deletions tests/compute/pack-any-value-8bit.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Test anyvalue packing of 8bit types.

//TEST_DISABLED(compute):COMPARE_COMPUTE_EX:-slang -compute -cuda -output-using-type
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -output-using-type -render-feature int16
//TEST_DISABLED(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 -profile sm_6_2 -use-dxil -output-using-type

[anyValueSize(20)]
interface IInterface
{
float run();
}

struct Val : IInterface
{
int8_t v0;
uint8_t v1;
float f0;
uint16_t v2;
uint8_t v3;
uint32_t v4;
uint8_t v5;
float run()
{
return v0 + v1 + f0 + v2 + v3 + v4 + v5;
}
};

struct UserDefinedPackedType
{
uint values[5];
};

//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=gOutputBuffer
RWStructuredBuffer<float> gOutputBuffer;

//TEST_INPUT: type_conformance Val:IInterface = 11

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
UserDefinedPackedType objStorage;
objStorage.values[0] = 0xA5A50201;
objStorage.values[1] = asuint(3.0f);
objStorage.values[2] = 4u|(0xA505u<<16u);
objStorage.values[3] = 6;
objStorage.values[4] = 7;

IInterface dynamicObj = createDynamicObject<IInterface, UserDefinedPackedType>(11, objStorage);
float result = dynamicObj.run();
gOutputBuffer[0] = result;

Val v;
v.v0 = 1;
v.v1 = 2;
v.f0 = 3;
v.v2 = 4;
v.v3 = 5;
v.v4 = 6;
v.v5 = 7;

IInterface dynamicObj1 = createDynamicObject<IInterface, Val>(11, v);;
gOutputBuffer[1] = dynamicObj1.run();

var packed = reinterpret<UserDefinedPackedType, Val>(v);
var unpacked = reinterpret<Val, UserDefinedPackedType>(packed);
gOutputBuffer[2] = unpacked.run();
}

4 changes: 4 additions & 0 deletions tests/compute/pack-any-value-8bit.slang.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: float
28.0
28.0
28.0