From 6e4b82741893be55f6216c31e19650029c667078 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 30 Jul 2024 20:28:34 -0700 Subject: Fixes for Metal ParameterBlock support. (#4752) --- source/slang/slang-ir.cpp | 71 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'source/slang/slang-ir.cpp') diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 88065cedc..1fc15f185 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -4870,6 +4870,27 @@ namespace Slang return inst; } + IRInst* IRBuilder::emitFieldExtract(IRInst* base, IRInst* fieldKey) + { + IRType* resultType = nullptr; + auto valueType = base->getDataType(); + auto structType = as(valueType); + SLANG_RELEASE_ASSERT(structType); + for (auto child : valueType->getChildren()) + { + auto field = as(child); + if (!field) + continue; + if (field->getKey() == fieldKey) + { + resultType = field->getFieldType(); + break; + } + } + SLANG_RELEASE_ASSERT(resultType); + return emitFieldExtract(resultType, base, fieldKey); + } + IRInst* IRBuilder::emitFieldExtract( IRType* type, IRInst* base, @@ -4902,6 +4923,40 @@ namespace Slang return type; } + IRInst* IRBuilder::emitFieldAddress( + IRInst* basePtr, + IRInst* fieldKey) + { + AddressSpace addrSpace = AddressSpace::Generic; + IRInst* valueType = nullptr; + auto basePtrType = unwrapAttributedType(basePtr->getDataType()); + if (auto ptrType = as(basePtrType)) + { + addrSpace = ptrType->getAddressSpace(); + valueType = ptrType->getValueType(); + } + else if (auto ptrLikeType = as(basePtrType)) + { + valueType = ptrLikeType->getElementType(); + } + IRType* resultType = nullptr; + auto structType = as(valueType); + SLANG_RELEASE_ASSERT(structType); + for (auto child : valueType->getChildren()) + { + auto field = as(child); + if (!field) + continue; + if (field->getKey() == fieldKey) + { + resultType = field->getFieldType(); + break; + } + } + SLANG_RELEASE_ASSERT(resultType); + return emitFieldAddress(getPtrType(kIROp_PtrType, resultType, addrSpace), basePtr, fieldKey); + } + IRInst* IRBuilder::emitFieldAddress( IRType* type, IRInst* base, @@ -5080,23 +5135,9 @@ namespace Slang { for (auto access : accessChain) { - auto basePtrType = cast(basePtr->getDataType()); - auto valueType = unwrapAttributedType(basePtrType->getValueType()); - IRType* resultType = nullptr; if (auto structKey = as(access)) { - auto structType = as(valueType); - SLANG_RELEASE_ASSERT(structType); - for (auto field : structType->getFields()) - { - if (field->getKey() == structKey) - { - resultType = field->getFieldType(); - break; - } - } - SLANG_RELEASE_ASSERT(resultType); - basePtr = emitFieldAddress(getPtrType(kIROp_PtrType, resultType, basePtrType->getAddressSpace()), basePtr, structKey); + basePtr = emitFieldAddress(basePtr, structKey); } else { -- cgit v1.2.3