Skip to content
Closed
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
6 changes: 6 additions & 0 deletions source/slang/core.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -2878,6 +2878,12 @@ __intrinsic_op(0)
[require(cpp_cuda_spirv_llvm)]
__prefix Ptr<T, Access::ReadWrite, AddressSpace::Device> operator&(__ref T value);

__generic<T>
__intrinsic_op(0)
[KnownBuiltin( $((int)KnownBuiltinDeclName::InternalAddressOf))]
[require(cpp_cuda_spirv_llvm)]
Ptr<T, Access::ReadWrite, AddressSpace::Device> __addressOf(__ref T value);

__generic<$(ptrTypeParameterList), TInt : __BuiltinIntegerType>
__intrinsic_op($(kIROp_GetOffsetPtr))
$(fullPtrType) operator+($(fullPtrType) value, TInt offset);
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-ast-support-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ FIDDLE() namespace Slang
IDifferentiablePtr,
NullDifferential,
OperatorAddressOf,
InternalAddressOf,
COUNT
};

Expand Down
4 changes: 3 additions & 1 deletion source/slang/slang-check-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3110,7 +3110,9 @@ Expr* SemanticsVisitor::CheckInvokeExprWithCheckedOperands(InvokeExpr* expr)
as<ConstantIntVal>(knownBuiltinAttr->name))
{
if (constantIntVal->getValue() ==
(int)KnownBuiltinDeclName::OperatorAddressOf)
(int)KnownBuiltinDeclName::OperatorAddressOf ||
constantIntVal->getValue() ==
(int)KnownBuiltinDeclName::InternalAddressOf)
{
getSink()->diagnose(
argExpr,
Expand Down
2 changes: 1 addition & 1 deletion source/standard-modules/neural/bindless-storage.slang
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct BindlessAddress<T> : IPointerLikeAddress<T>
{
case cuda:
// On CUDA, use packed vector atomic for types that support it (half2, bfloat16x2).
let scalarPtr = &handle[baseIndex + index];
let scalarPtr = __addressOf(handle[baseIndex + index]);
let vecPtr = reinterpret<vector<T, 2>*>(scalarPtr);
__atomic_reduce_add(vecPtr[0], value);
default:
Expand Down
2 changes: 1 addition & 1 deletion tests/bugs/gh-3601.slang
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void funcThatTakesPointer(PP* p)
}
int* funcThatReturnsPointer(PP* p)
{
return &p.data;
return __addressOf(p.data);
}

// CHECK: OpEntryPoint
Expand Down
2 changes: 1 addition & 1 deletion tests/bugs/gh-7499.slang
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export __extern_cpp int main()
// Success is not crashing the compiler.
int size = 0;
int arr = 0;
f(&arr, size);
f(__addressOf(arr), size);

return 0;
}
2 changes: 1 addition & 1 deletion tests/bugs/gh-8659.slang
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RWStructuredBuffer<uint> outputBuffer;
[numthreads(1,1,1)]
void computeMain()
{
let indices = (uint*)&ptr[0];
let indices = (uint*)__addressOf(ptr[0]);
*indices = 100;
// CHECK: 100
outputBuffer[0] = ptr[0];
Expand Down
20 changes: 10 additions & 10 deletions tests/cpu-program/gfx-smoke.slang
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export __extern_cpp int main()
gfx.DeviceDesc deviceDesc = {};
deviceDesc.deviceType = gfx.DeviceType.CPU;
Optional<gfx.IDevice> device;
gfx.gfxCreateDevice(&deviceDesc, device);
gfx.gfxCreateDevice(__addressOf(deviceDesc), device);
if (device == none)
{
printf("fail\n");
Expand All @@ -17,7 +17,7 @@ export __extern_cpp int main()
gfx.CommandQueueDesc queueDesc = {gfx::QueueType::Graphics};
queueDesc.type = gfx.QueueType.Graphics;
Optional<gfx.ICommandQueue> queue;
device.value.createCommandQueue(&queueDesc, queue);
device.value.createCommandQueue(__addressOf(queueDesc), queue);

gfx.ShaderProgramDesc2 programDesc = {};
NativeString s = R"(
Expand All @@ -36,15 +36,15 @@ export __extern_cpp int main()
programDesc.sourceDataSize = s.length;
programDesc.entryPointCount = 1;
NativeString entryPointName = "computeMain";
programDesc.entryPointNames = &entryPointName;
programDesc.entryPointNames = __addressOf(entryPointName);
Optional<gfx.IShaderProgram> program;
Optional<slang.ISlangBlob> diagBlob;
device.value.createProgram2(&programDesc, program, diagBlob);
device.value.createProgram2(__addressOf(programDesc), program, diagBlob);

Optional<gfx.IPipelineState> pipeline;
gfx.ComputePipelineStateDesc pipelineDesc;
pipelineDesc.program = NativeRef<gfx.IShaderProgram>(program.value);
device.value.createComputePipelineState(&pipelineDesc, pipeline);
device.value.createComputePipelineState(__addressOf(pipelineDesc), pipeline);

Optional<gfx.ITransientResourceHeap> transientHeap;
gfx.TransientResourceHeapDesc transientHeapDesc;
Expand All @@ -54,7 +54,7 @@ export __extern_cpp int main()
transientHeapDesc.uavDescriptorCount = 1024;
transientHeapDesc.samplerDescriptorCount = 256;
transientHeapDesc.accelerationStructureDescriptorCount = 32;
device.value.createTransientResourceHeap(&transientHeapDesc, transientHeap);
device.value.createTransientResourceHeap(__addressOf(transientHeapDesc), transientHeap);

Optional<gfx.IBufferResource> buffer;
gfx.BufferResourceDesc bufferDesc = {};
Expand All @@ -64,12 +64,12 @@ export __extern_cpp int main()
bufferDesc.elementSize = 4;
bufferDesc.sizeInBytes = 256;
bufferDesc.type = gfx.ResourceType.Buffer;
device.value.createBufferResource(&bufferDesc, nullptr, buffer);
device.value.createBufferResource(__addressOf(bufferDesc), nullptr, buffer);

Optional<gfx.IResourceView> bufferView;
gfx.ResourceViewDesc viewDesc;
viewDesc.type = gfx.ResourceViewType.UnorderedAccess;
device.value.createBufferView(buffer.value, none, &viewDesc, bufferView);
device.value.createBufferView(buffer.value, none, __addressOf(viewDesc), bufferView);

Optional<gfx.ICommandBuffer> commandBuffer;
transientHeap.value.createCommandBuffer(commandBuffer);
Expand All @@ -80,13 +80,13 @@ export __extern_cpp int main()
Optional<gfx.IShaderObject> entryPointObject;
rootObject.value.getEntryPoint(0, entryPointObject);
gfx.ShaderOffset offset = {};
entryPointObject.value.setResource(&offset, bufferView.value);
entryPointObject.value.setResource(__addressOf(offset), bufferView.value);
encoder.value.dispatchCompute(1, 1, 1);
encoder.value.endEncoding();
commandBuffer.value.close();

NativeRef<gfx.ICommandBuffer> commandBufferRef = NativeRef<gfx.ICommandBuffer>(commandBuffer.value);
queue.value.executeCommandBuffers(1, &commandBufferRef, none, 0);
queue.value.executeCommandBuffers(1, __addressOf(commandBufferRef), none, 0);
queue.value.waitOnHost();

Optional<slang.ISlangBlob> blob;
Expand Down
4 changes: 2 additions & 2 deletions tests/cpu-program/pointer-basics.slang
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
export __extern_cpp int main()
{
uint2 value;
int *pValue = (int*)&value;
int *pValue = (int*)__addressOf(value);
*pValue = 1;
(*pValue)++;
++pValue[0];
++pValue;
*pValue = 1;
pValue = (int *)&value;
pValue = (int *)__addressOf(value);
int64_t ptrVal = int64_t(pValue);
pValue = (int *)ptrVal;
if (pValue
Expand Down
2 changes: 1 addition & 1 deletion tests/cpu-program/pointer-deref.slang
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Record
export __extern_cpp int main()
{
Record rec;
Record *pRec = &rec;
Record *pRec = __addressOf(rec);
pRec.field = 1;
pRec.sub.field2 = 2;
pRec.sub.field3 = 3.0f;
Expand Down
4 changes: 2 additions & 2 deletions tests/diagnostics/invalid-constant-pointer-taking.slang
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ StructuredBuffer<uint> constant_uint_buffer;
[numthreads(1,1,1)]
void computeMain(uint3 threadId : SV_DispatchThreadID)
{
float* mutablePtr1 = &mutable_float_buffer[threadId.x];
float* mutablePtr1 = __addressOf(mutable_float_buffer[threadId.x]);

// CHECK: ([[# @LINE+1]]): error 31160
float* mutablePtr2 = __getAddress(mutable_float_buffer[threadId.x]);
Expand All @@ -20,7 +20,7 @@ void computeMain(uint3 threadId : SV_DispatchThreadID)

// Constant pointers arent a thing in slang
// CHECK: ([[# @LINE+1]]): error 30079:
float* ptr1 = &constant_float_buffer[threadId.x];
float* ptr1 = __addressOf(constant_float_buffer[threadId.x]);

// CHECK: ([[# @LINE+1]]): error 31160
float* ptr2 = __getAddress(constant_float_buffer[threadId.x]);
Expand Down
2 changes: 1 addition & 1 deletion tests/language-feature/bitfield/msvc-repr.slang
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ void computeMain()
s.d = 0xDEF0;

// Write the struct to memory and read it back as uint
outputBuffer[0] = *((uint*)&s);
outputBuffer[0] = *((uint*)__addressOf(s));
}

2 changes: 1 addition & 1 deletion tests/language-feature/bitfield/repr-mixed.slang
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ void computeMain()
m.d = 0xEF;

// With default packing, all fields are in one backing field
outputBuffer[0] = *((uint*)&m) & 0xFFFFFF; // Mask to 24 bits
outputBuffer[0] = *((uint*)__addressOf(m)) & 0xFFFFFF; // Mask to 24 bits
}

2 changes: 1 addition & 1 deletion tests/language-feature/bitfield/repr.slang
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ void computeMain()
s.d = 0xDEF0;

// Write the struct to memory and read it back as uint
outputBuffer[0] = *((uint*)&s);
outputBuffer[0] = *((uint*)__addressOf(s));
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ void computeMain()
Ptr<int> ptrIn = inputBuffer;
Ptr<int> secondPtrIn = ptrIn;
Ptr<int> ptrOut = outputBuffer;
storeCoherent<4, MemoryScope::Device>(ptrOut, loadCoherent<4, MemoryScope::Device>(&secondPtrIn[1]));
storeCoherent<4, MemoryScope::Device>(ptrOut, loadCoherent<4, MemoryScope::Device>(__addressOf(secondPtrIn[1])));
}
2 changes: 1 addition & 1 deletion tests/llvm/inline-llvm-ir.slang
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
float4 a = float4(1,2,3,4);
float f1 = reduceAdd(a);
memset(Ptr<void>(&a), 0, 8);
memset(Ptr<void>(__addressOf(a)), 0, 8);
float f2 = reduceAdd(a);

// CHECK: 10.0
Expand Down
2 changes: 1 addition & 1 deletion tests/spirv/pointer.slang
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void funcThatTakesPointer(PP* p)
}
int* funcThatReturnsPointer(PP* p)
{
return &p.data;
return __addressOf(p.data);
}

void funcWithInOutParam(inout PP p)
Expand Down