Skip to content

Commit

Permalink
Fix Ptr::__subscript to accept any integer index. (shader-slang#4100)
Browse files Browse the repository at this point in the history
* Fix `Ptr::__subscript` to accept any integer index.

* Fix `Ptr::__subscript` to allow 64bit indices.
  • Loading branch information
csyonghe authored May 3, 2024
1 parent 13250ff commit 47a917c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/slang/core.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ struct Ptr
__intrinsic_op($(kIROp_CastIntToPtr))
__init(int64_t val);

__subscript(int index) -> T
__generic<TInt : __BuiltinIntegerType>
__subscript(TInt index) -> T
{
__intrinsic_op($(kIROp_GetOffsetPtr))
ref;
Expand Down
4 changes: 4 additions & 0 deletions source/slang/slang-lower-to-ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10050,6 +10050,10 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
{
return ensureDecl(context, typedefDecl);
}
else if (auto subscriptDecl = as<SubscriptDecl>(genDecl->inner))
{
return ensureDecl(context, subscriptDecl);
}
SLANG_RELEASE_ASSERT(false);
UNREACHABLE_RETURN(LoweredValInfo());
}
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4311,6 +4311,7 @@ namespace Slang
case ASTNodeType::TypeAliasDecl:
case ASTNodeType::TypeDefDecl:
case ASTNodeType::ExtensionDecl:
case ASTNodeType::SubscriptDecl:
return true;
default:
return false;
Expand Down
12 changes: 12 additions & 0 deletions tests/spirv/ptr-subscript.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry main -stage compute -emit-spirv-directly

// CHECK: OpEntryPoint

ConstantBuffer<Ptr<int>> cbPtr;
void main(int id : SV_DispatchThreadID)
{
// Check that the index operand is translated directly into a 64bit integer
// in th resulting SPIR-V without any truncations.
// CHECK: OpPtrAccessChain %_ptr_PhysicalStorageBuffer_int %{{.*}} %long_123
cbPtr[123ll] = 4;
}

0 comments on commit 47a917c

Please sign in to comment.