From 1300678a36bf8d7081647aef3e2a37dbd496aaf5 Mon Sep 17 00:00:00 2001 From: Ronan Date: Wed, 8 Oct 2025 11:19:13 +0200 Subject: `ExprLoweringVisitorBase::getDefaultVal(Type*)` use `MakeVector/MatrixFromScalar` (#8512) - Allows using `Vector/Matrix` type with yet unresolved dimensions - Simpler implementation and in-line with default `Array` - Added `test/bugs/gh-8512.slang` --- source/slang/slang-lower-to-ir.cpp | 25 ++++-------------------- tests/bugs/gh-8512-2.slang | 40 ++++++++++++++++++++++++++++++++++++++ tests/bugs/gh-8512.slang | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 tests/bugs/gh-8512-2.slang create mode 100644 tests/bugs/gh-8512.slang diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index d3f7d4e4c..c4492cfbb 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -4948,34 +4948,17 @@ struct ExprLoweringVisitorBase : public ExprVisitor } else if (auto vectorType = as(type)) { - UInt elementCount = (UInt)getIntVal(vectorType->getElementCount()); - auto irDefaultValue = getSimpleVal(context, getDefaultVal(vectorType->getElementType())); - - List args; - for (UInt ee = 0; ee < elementCount; ++ee) - { - args.add(irDefaultValue); - } return LoweredValInfo::simple( - getBuilder()->emitMakeVector(irType, args.getCount(), args.getBuffer())); + getBuilder()->emitMakeVectorFromScalar(irType, irDefaultValue)); } else if (auto matrixType = as(type)) { - UInt rowCount = (UInt)getIntVal(matrixType->getRowCount()); - - auto rowType = matrixType->getRowType(); - - auto irDefaultValue = getSimpleVal(context, getDefaultVal(rowType)); - - List args; - for (UInt rr = 0; rr < rowCount; ++rr) - { - args.add(irDefaultValue); - } + auto irDefaultValue = + getSimpleVal(context, getDefaultVal(matrixType->getElementType())); return LoweredValInfo::simple( - getBuilder()->emitMakeMatrix(irType, args.getCount(), args.getBuffer())); + getBuilder()->emitMakeMatrixFromScalar(irType, irDefaultValue)); } else if (auto arrayType = as(type)) { diff --git a/tests/bugs/gh-8512-2.slang b/tests/bugs/gh-8512-2.slang new file mode 100644 index 000000000..4c51af696 --- /dev/null +++ b/tests/bugs/gh-8512-2.slang @@ -0,0 +1,40 @@ +//TEST:INTERPRET(filecheck=ICHECK): + +__generic +struct Spectrum +{ + static const int Samples = Mode & 0xFF; + typealias VecT = vector; + VecT data; + + static const bool IsRGB = (Mode & 0x100) != 0; + + __generic + __init(vector v) + { + this.data = VecT(v); + } + + static This MakeFromRGB(vector rgb) + { + if(IsRGB) + { + return (Spectrum(rgb) as This).value; + } + else + { + return {}; + } + } +} + +static const int DefaultMode = 0x103; +typealias Spec = Spectrum; + +void main() +{ + float3 color = {1, 2, 3}; + let spec = Spec::MakeFromRGB(color); + // ICHECK: (1, 2, 3) + printf("(%.0f, %.0f, %.0f)", spec.data[0], spec.data[1], spec.data[2]); +} diff --git a/tests/bugs/gh-8512.slang b/tests/bugs/gh-8512.slang new file mode 100644 index 000000000..e94d95719 --- /dev/null +++ b/tests/bugs/gh-8512.slang @@ -0,0 +1,39 @@ +//TEST:SIMPLE(filecheck=CHECK): -target spirv +// CHECK: OpEntryPoint + +__generic +struct Spectrum +{ + static const int Samples = Mode & 0xFF; + typealias VecT = vector; + VecT data; + + static const bool IsRGB = (Mode & 0x100) != 0; + + __generic + __init(vector v) + { + this.data = VecT(v); + } + + static This MakeFromRGB(vector rgb) + { + if(IsRGB) + { + return (Spectrum(rgb) as This).value; + } + else + { + return {}; + } + } +} + +static const int DefaultMode = 0x103; +typealias Spec = Spectrum; + +[shader("vertex")] +Spec main(float3 vertex_color : COLOR0) : COLOR0 +{ + return Spec::MakeFromRGB(vertex_color); +} -- cgit v1.2.3