From 152bc3771daf060414ee0ca9f0884630a21beaf2 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sun, 14 Nov 2021 12:53:11 +0100 Subject: [PATCH 01/12] Revive the array location fix --- .../src/codegen_cx/entry.rs | 9 ++- tests/ui/dis/array_location_calculation.rs | 7 ++ .../ui/dis/array_location_calculation.stderr | 78 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/ui/dis/array_location_calculation.rs create mode 100644 tests/ui/dis/array_location_calculation.stderr diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 75c8f845fd..3cd386e114 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -535,7 +535,14 @@ impl<'tcx> CodegenCx<'tcx> { Decoration::Location, std::iter::once(Operand::LiteralInt32(*location)), ); - *location += 1; + // Arrays take up multiple locations + *location += if let SpirvType::Array { count, .. } = self.lookup_type(value_spirv_type) { + self.builder + .lookup_const_u64(count) + .expect("Array type has invalid count value") as u32 + } else { + 1 + } } // Emit the `OpVariable` with its *Result* ID set to `var`. diff --git a/tests/ui/dis/array_location_calculation.rs b/tests/ui/dis/array_location_calculation.rs new file mode 100644 index 0000000000..169e4a3bf6 --- /dev/null +++ b/tests/ui/dis/array_location_calculation.rs @@ -0,0 +1,7 @@ +// build-pass +// compile-flags: -C llvm-args=--disassemble-fn=add_two_ints::add_two_ints + +use spirv_std as _; + +#[spirv(fragment)] +pub fn array_locations(one: [f32; 7], two: [f32; 3], three: f32) {} diff --git a/tests/ui/dis/array_location_calculation.stderr b/tests/ui/dis/array_location_calculation.stderr new file mode 100644 index 0000000000..e39504a8a6 --- /dev/null +++ b/tests/ui/dis/array_location_calculation.stderr @@ -0,0 +1,78 @@ +thread 'rustc' panicked at 'no function with the name `add_two_ints::add_two_ints` found in: +; SPIR-V +; Version: 1.3 +; Generator: Unknown +; Bound: 28 +OpCapability Float64 +OpCapability Int16 +OpCapability Int64 +OpCapability Int8 +OpCapability ShaderClockKHR +OpCapability Shader +OpCapability VulkanMemoryModel +OpExtension "SPV_KHR_shader_clock" +OpExtension "SPV_KHR_vulkan_memory_model" +OpMemoryModel Logical Vulkan +OpEntryPoint Fragment %1 "array_locations" %2 %3 %4 +OpExecutionMode %1 OriginUpperLeft +%5 = OpString "C:/Users/Ashley/Desktop/rust-gpu/tests/ui/dis/array_location_calculation.rs" +OpName %6 "array_location_calculation::array_locations" +OpName %2 "one" +OpName %3 "two" +OpName %4 "three" +OpDecorate %7 ArrayStride 4 +OpDecorate %8 ArrayStride 4 +OpDecorate %2 Location 0 +OpDecorate %3 Location 7 +OpDecorate %4 Location 10 +%9 = OpTypeVoid +%10 = OpTypeFloat 32 +%11 = OpTypeInt 32 0 +%12 = OpConstant %11 7 +%7 = OpTypeArray %10 %12 +%13 = OpConstant %11 3 +%8 = OpTypeArray %10 %13 +%14 = OpTypeFunction %9 %7 %8 %10 +%15 = OpTypeFunction %9 +%16 = OpTypePointer Input %7 +%2 = OpVariable %16 Input +%17 = OpTypePointer Input %8 +%3 = OpVariable %17 Input +%18 = OpTypePointer Input %10 +%4 = OpVariable %18 Input +%6 = OpFunction %9 None %14 +%19 = OpFunctionParameter %7 +%20 = OpFunctionParameter %8 +%21 = OpFunctionParameter %10 +%22 = OpLabel +OpLine %5 7 67 +OpReturn +OpFunctionEnd +%1 = OpFunction %9 None %15 +%23 = OpLabel +OpLine %5 7 23 +%24 = OpLoad %7 %2 +OpLine %5 7 38 +%25 = OpLoad %8 %3 +OpLine %5 7 53 +%26 = OpLoad %10 %4 +OpLine %5 7 0 +%27 = OpFunctionCall %9 %6 %24 %25 %26 +OpReturn +OpFunctionEnd +', crates/rustc_codegen_spirv/src/codegen_cx/mod.rs:378:21 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +error: internal compiler error: unexpected panic + +note: the compiler unexpectedly panicked. this is a bug. + +note: we would appreciate a bug report: https://github.com/EmbarkStudios/rust-gpu/issues/new + +note: rustc 1.58.0-nightly (29b124802 2021-10-25) running on x86_64-pc-windows-msvc + +note: compiler flags: -Z codegen-backend=C:/Users/Ashley/Desktop/rust-gpu/target/release/deps/rustc_codegen_spirv.dll -Z symbol-mangling-version=v0 -Z unstable-options -Z crate-attr=no_std -Z crate-attr=feature(register_attr,asm) -Z crate-attr=register_attr(spirv) -C prefer-dynamic -C overflow-checks=off -C debug-assertions=off -C debuginfo=2 -C embed-bitcode=no -C target-feature=+Int8,+Int16,+Int64,+Float64,+ShaderClockKHR,+ext:SPV_KHR_shader_clock -C llvm-args=--disassemble-fn=add_two_ints::add_two_ints --crate-type dylib + +query stack during panic: +end of query stack +note: `rust-gpu` version 0.4.0-alpha.12 + From 605f16668454ccd88f2a76f47057d864ba1bcbce Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sun, 14 Nov 2021 13:02:02 +0100 Subject: [PATCH 02/12] Properly run test --- tests/ui/dis/array_location_calculation.rs | 4 +- .../ui/dis/array_location_calculation.stderr | 47 ++----------------- 2 files changed, 5 insertions(+), 46 deletions(-) diff --git a/tests/ui/dis/array_location_calculation.rs b/tests/ui/dis/array_location_calculation.rs index 169e4a3bf6..1e7d23b888 100644 --- a/tests/ui/dis/array_location_calculation.rs +++ b/tests/ui/dis/array_location_calculation.rs @@ -1,7 +1,7 @@ // build-pass -// compile-flags: -C llvm-args=--disassemble-fn=add_two_ints::add_two_ints +// compile-flags: -C llvm-args=--disassemble-globals use spirv_std as _; #[spirv(fragment)] -pub fn array_locations(one: [f32; 7], two: [f32; 3], three: f32) {} +pub fn main(one: [f32; 7], two: [f32; 3], three: f32) {} diff --git a/tests/ui/dis/array_location_calculation.stderr b/tests/ui/dis/array_location_calculation.stderr index e39504a8a6..497e811671 100644 --- a/tests/ui/dis/array_location_calculation.stderr +++ b/tests/ui/dis/array_location_calculation.stderr @@ -1,8 +1,3 @@ -thread 'rustc' panicked at 'no function with the name `add_two_ints::add_two_ints` found in: -; SPIR-V -; Version: 1.3 -; Generator: Unknown -; Bound: 28 OpCapability Float64 OpCapability Int16 OpCapability Int64 @@ -13,10 +8,10 @@ OpCapability VulkanMemoryModel OpExtension "SPV_KHR_shader_clock" OpExtension "SPV_KHR_vulkan_memory_model" OpMemoryModel Logical Vulkan -OpEntryPoint Fragment %1 "array_locations" %2 %3 %4 +OpEntryPoint Fragment %1 "main" %2 %3 %4 OpExecutionMode %1 OriginUpperLeft -%5 = OpString "C:/Users/Ashley/Desktop/rust-gpu/tests/ui/dis/array_location_calculation.rs" -OpName %6 "array_location_calculation::array_locations" +%5 = OpString "$OPSTRING_FILENAME/array_location_calculation.rs" +OpName %6 "array_location_calculation::main" OpName %2 "one" OpName %3 "two" OpName %4 "three" @@ -40,39 +35,3 @@ OpDecorate %4 Location 10 %3 = OpVariable %17 Input %18 = OpTypePointer Input %10 %4 = OpVariable %18 Input -%6 = OpFunction %9 None %14 -%19 = OpFunctionParameter %7 -%20 = OpFunctionParameter %8 -%21 = OpFunctionParameter %10 -%22 = OpLabel -OpLine %5 7 67 -OpReturn -OpFunctionEnd -%1 = OpFunction %9 None %15 -%23 = OpLabel -OpLine %5 7 23 -%24 = OpLoad %7 %2 -OpLine %5 7 38 -%25 = OpLoad %8 %3 -OpLine %5 7 53 -%26 = OpLoad %10 %4 -OpLine %5 7 0 -%27 = OpFunctionCall %9 %6 %24 %25 %26 -OpReturn -OpFunctionEnd -', crates/rustc_codegen_spirv/src/codegen_cx/mod.rs:378:21 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -error: internal compiler error: unexpected panic - -note: the compiler unexpectedly panicked. this is a bug. - -note: we would appreciate a bug report: https://github.com/EmbarkStudios/rust-gpu/issues/new - -note: rustc 1.58.0-nightly (29b124802 2021-10-25) running on x86_64-pc-windows-msvc - -note: compiler flags: -Z codegen-backend=C:/Users/Ashley/Desktop/rust-gpu/target/release/deps/rustc_codegen_spirv.dll -Z symbol-mangling-version=v0 -Z unstable-options -Z crate-attr=no_std -Z crate-attr=feature(register_attr,asm) -Z crate-attr=register_attr(spirv) -C prefer-dynamic -C overflow-checks=off -C debug-assertions=off -C debuginfo=2 -C embed-bitcode=no -C target-feature=+Int8,+Int16,+Int64,+Float64,+ShaderClockKHR,+ext:SPV_KHR_shader_clock -C llvm-args=--disassemble-fn=add_two_ints::add_two_ints --crate-type dylib - -query stack during panic: -end of query stack -note: `rust-gpu` version 0.4.0-alpha.12 - From 52ac5f5b03ee7586c6b9f6b5b84ea602494effd7 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sun, 14 Nov 2021 13:30:55 +0100 Subject: [PATCH 03/12] Use a recursive function for calculating the number of locations a struct takes up --- .../src/codegen_cx/entry.rs | 38 ++++++-- tests/ui/dis/array_location_calculation.rs | 4 +- .../ui/dis/array_location_calculation.stderr | 93 ++++++++++++++----- 3 files changed, 102 insertions(+), 33 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 3cd386e114..65870adabb 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -535,14 +535,7 @@ impl<'tcx> CodegenCx<'tcx> { Decoration::Location, std::iter::once(Operand::LiteralInt32(*location)), ); - // Arrays take up multiple locations - *location += if let SpirvType::Array { count, .. } = self.lookup_type(value_spirv_type) { - self.builder - .lookup_const_u64(count) - .expect("Array type has invalid count value") as u32 - } else { - 1 - } + *location += self.location_size_of_type(value_spirv_type); } // Emit the `OpVariable` with its *Result* ID set to `var`. @@ -561,4 +554,33 @@ impl<'tcx> CodegenCx<'tcx> { } } } + + fn location_size_of_type(&self, ty: Word) -> u32 { + match self.lookup_type(ty) { + // Arrays take up multiple locations.git + SpirvType::Array { count, .. } => { + self.builder + .lookup_const_u64(count) + .expect("Array type has invalid count value") as u32 + } + // Structs take up per component. + SpirvType::Adt { field_types, .. } => { + let mut size = 0; + + for field_type in field_types { + size += self.location_size_of_type(field_type); + } + + size + } + // 2 component vectors always take up 1 location. + SpirvType::Vector { count: 2, .. } => 1, + // 3 or 4 component vectors take up 2 locations if they have a 64-bit scalar type. + SpirvType::Vector { element, .. } => match self.lookup_type(element) { + SpirvType::Float(64) | Spirv::Integer(64, _) => 2, + _ => 1, + }, + _ => 1, + } + } } diff --git a/tests/ui/dis/array_location_calculation.rs b/tests/ui/dis/array_location_calculation.rs index 1e7d23b888..cbd64f8f1f 100644 --- a/tests/ui/dis/array_location_calculation.rs +++ b/tests/ui/dis/array_location_calculation.rs @@ -1,7 +1,7 @@ // build-pass // compile-flags: -C llvm-args=--disassemble-globals -use spirv_std as _; +use spirv_std::{self as _, glam::{Mat3, DVec3, IVec4}}; #[spirv(fragment)] -pub fn main(one: [f32; 7], two: [f32; 3], three: f32) {} +pub fn main(one: [f32; 7], two: [f32; 3], three: Mat3, four: DVec3, five: IVec4, six: f32, seven: u32) {} diff --git a/tests/ui/dis/array_location_calculation.stderr b/tests/ui/dis/array_location_calculation.stderr index 497e811671..98ddd275c8 100644 --- a/tests/ui/dis/array_location_calculation.stderr +++ b/tests/ui/dis/array_location_calculation.stderr @@ -1,37 +1,84 @@ +[crates/rustc_codegen_spirv/src/codegen_cx/entry.rs:583] other = Integer( + 32, + true, +) +[crates/rustc_codegen_spirv/src/codegen_cx/entry.rs:589] other = Integer( + 32, + false, +) OpCapability Float64 OpCapability Int16 OpCapability Int64 OpCapability Int8 OpCapability ShaderClockKHR OpCapability Shader -OpCapability VulkanMemoryModel OpExtension "SPV_KHR_shader_clock" -OpExtension "SPV_KHR_vulkan_memory_model" -OpMemoryModel Logical Vulkan -OpEntryPoint Fragment %1 "main" %2 %3 %4 +OpMemoryModel Logical Simple +OpEntryPoint Fragment %1 "main" %2 %3 %4 %5 %6 %7 %8 OpExecutionMode %1 OriginUpperLeft -%5 = OpString "$OPSTRING_FILENAME/array_location_calculation.rs" -OpName %6 "array_location_calculation::main" +%9 = OpString "$OPSTRING_FILENAME/array_location_calculation.rs" +OpMemberName %10 0 "x_axis" +OpMemberName %10 1 "y_axis" +OpMemberName %10 2 "z_axis" +OpName %10 "spirv_std::glam::core::storage::Columns3>" +OpMemberName %11 0 "0" +OpName %11 "spirv_std::glam::Mat3" +OpName %12 "array_location_calculation::main" OpName %2 "one" OpName %3 "two" OpName %4 "three" -OpDecorate %7 ArrayStride 4 -OpDecorate %8 ArrayStride 4 +OpName %5 "four" +OpName %6 "five" +OpName %7 "six" +OpName %8 "seven" +OpMemberName %10 0 "x_axis" +OpMemberName %10 1 "y_axis" +OpMemberName %10 2 "z_axis" +OpMemberName %11 0 "0" +OpMemberName %11 0 "0" +OpMemberName %10 0 "x_axis" +OpMemberName %10 1 "y_axis" +OpMemberName %10 2 "z_axis" +OpDecorate %13 ArrayStride 4 +OpDecorate %14 ArrayStride 4 +OpMemberDecorate %10 0 Offset 0 +OpMemberDecorate %10 1 Offset 16 +OpMemberDecorate %10 2 Offset 32 +OpMemberDecorate %11 0 Offset 0 OpDecorate %2 Location 0 OpDecorate %3 Location 7 OpDecorate %4 Location 10 -%9 = OpTypeVoid -%10 = OpTypeFloat 32 -%11 = OpTypeInt 32 0 -%12 = OpConstant %11 7 -%7 = OpTypeArray %10 %12 -%13 = OpConstant %11 3 -%8 = OpTypeArray %10 %13 -%14 = OpTypeFunction %9 %7 %8 %10 -%15 = OpTypeFunction %9 -%16 = OpTypePointer Input %7 -%2 = OpVariable %16 Input -%17 = OpTypePointer Input %8 -%3 = OpVariable %17 Input -%18 = OpTypePointer Input %10 -%4 = OpVariable %18 Input +OpDecorate %5 Location 13 +OpDecorate %6 Location 15 +OpDecorate %7 Location 16 +OpDecorate %8 Location 17 +%15 = OpTypeVoid +%16 = OpTypeFloat 32 +%17 = OpTypeInt 32 0 +%18 = OpConstant %17 7 +%13 = OpTypeArray %16 %18 +%19 = OpConstant %17 3 +%14 = OpTypeArray %16 %19 +%20 = OpTypeVector %16 3 +%10 = OpTypeStruct %20 %20 %20 +%11 = OpTypeStruct %10 +%21 = OpTypeFloat 64 +%22 = OpTypeVector %21 3 +%23 = OpTypeInt 32 1 +%24 = OpTypeVector %23 4 +%25 = OpTypeFunction %15 %13 %14 %11 %22 %24 %16 %17 +%26 = OpTypeFunction %15 +%27 = OpTypePointer Input %13 +%2 = OpVariable %27 Input +%28 = OpTypePointer Input %14 +%3 = OpVariable %28 Input +%29 = OpTypePointer Input %11 +%4 = OpVariable %29 Input +%30 = OpTypePointer Input %22 +%5 = OpVariable %30 Input +%31 = OpTypePointer Input %24 +%6 = OpVariable %31 Input +%32 = OpTypePointer Input %16 +%7 = OpVariable %32 Input +%33 = OpTypePointer Input %17 +%8 = OpVariable %33 Input From 6df36f40afd3ad9bc8e49a1ec669bde37f6d4af5 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sun, 14 Nov 2021 13:37:49 +0100 Subject: [PATCH 04/12] Remove debugging from the stderr --- crates/rustc_codegen_spirv/src/codegen_cx/entry.rs | 4 ++-- tests/ui/dis/array_location_calculation.stderr | 12 +++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 65870adabb..7dff4398f9 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -557,7 +557,7 @@ impl<'tcx> CodegenCx<'tcx> { fn location_size_of_type(&self, ty: Word) -> u32 { match self.lookup_type(ty) { - // Arrays take up multiple locations.git + // Arrays take up multiple locations. SpirvType::Array { count, .. } => { self.builder .lookup_const_u64(count) @@ -577,7 +577,7 @@ impl<'tcx> CodegenCx<'tcx> { SpirvType::Vector { count: 2, .. } => 1, // 3 or 4 component vectors take up 2 locations if they have a 64-bit scalar type. SpirvType::Vector { element, .. } => match self.lookup_type(element) { - SpirvType::Float(64) | Spirv::Integer(64, _) => 2, + SpirvType::Float(64) | SpirvType::Integer(64, _) => 2, _ => 1, }, _ => 1, diff --git a/tests/ui/dis/array_location_calculation.stderr b/tests/ui/dis/array_location_calculation.stderr index 98ddd275c8..a40d1b8353 100644 --- a/tests/ui/dis/array_location_calculation.stderr +++ b/tests/ui/dis/array_location_calculation.stderr @@ -1,19 +1,13 @@ -[crates/rustc_codegen_spirv/src/codegen_cx/entry.rs:583] other = Integer( - 32, - true, -) -[crates/rustc_codegen_spirv/src/codegen_cx/entry.rs:589] other = Integer( - 32, - false, -) OpCapability Float64 OpCapability Int16 OpCapability Int64 OpCapability Int8 OpCapability ShaderClockKHR OpCapability Shader +OpCapability VulkanMemoryModel OpExtension "SPV_KHR_shader_clock" -OpMemoryModel Logical Simple +OpExtension "SPV_KHR_vulkan_memory_model" +OpMemoryModel Logical Vulkan OpEntryPoint Fragment %1 "main" %2 %3 %4 %5 %6 %7 %8 OpExecutionMode %1 OriginUpperLeft %9 = OpString "$OPSTRING_FILENAME/array_location_calculation.rs" From e5fc2a56e4b6b8a82946103d06017632fcfa15a5 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sun, 14 Nov 2021 13:50:32 +0100 Subject: [PATCH 05/12] Handle matrices --- .../src/codegen_cx/entry.rs | 1 + tests/ui/dis/array_location_calculation.rs | 24 +++- .../ui/dis/array_location_calculation.stderr | 112 +++++++++--------- 3 files changed, 81 insertions(+), 56 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 7dff4398f9..a0d17a9396 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -580,6 +580,7 @@ impl<'tcx> CodegenCx<'tcx> { SpirvType::Float(64) | SpirvType::Integer(64, _) => 2, _ => 1, }, + SpirvType::Matrix { element, count } => count * self.location_size_of_type(element), _ => 1, } } diff --git a/tests/ui/dis/array_location_calculation.rs b/tests/ui/dis/array_location_calculation.rs index cbd64f8f1f..2ff8bc56f3 100644 --- a/tests/ui/dis/array_location_calculation.rs +++ b/tests/ui/dis/array_location_calculation.rs @@ -1,7 +1,27 @@ // build-pass // compile-flags: -C llvm-args=--disassemble-globals -use spirv_std::{self as _, glam::{Mat3, DVec3, IVec4}}; +use spirv_std::{ + self as _, + glam::{DVec3, IVec4, Mat3, Vec4}, +}; + +#[spirv(matrix)] +pub struct Mat4x3 { + pub col_0: Vec4, + pub col_1: Vec4, + pub col_2: Vec4, +} #[spirv(fragment)] -pub fn main(one: [f32; 7], two: [f32; 3], three: Mat3, four: DVec3, five: IVec4, six: f32, seven: u32) {} +pub fn main( + one: [f32; 7], + two: [f32; 3], + three: Mat3, + four: DVec3, + five: IVec4, + six: f32, + seven: Mat4x3, + eight: u32, +) { +} diff --git a/tests/ui/dis/array_location_calculation.stderr b/tests/ui/dis/array_location_calculation.stderr index a40d1b8353..8a5a05bf88 100644 --- a/tests/ui/dis/array_location_calculation.stderr +++ b/tests/ui/dis/array_location_calculation.stderr @@ -4,20 +4,18 @@ OpCapability Int64 OpCapability Int8 OpCapability ShaderClockKHR OpCapability Shader -OpCapability VulkanMemoryModel OpExtension "SPV_KHR_shader_clock" -OpExtension "SPV_KHR_vulkan_memory_model" -OpMemoryModel Logical Vulkan -OpEntryPoint Fragment %1 "main" %2 %3 %4 %5 %6 %7 %8 +OpMemoryModel Logical Simple +OpEntryPoint Fragment %1 "main" %2 %3 %4 %5 %6 %7 %8 %9 OpExecutionMode %1 OriginUpperLeft -%9 = OpString "$OPSTRING_FILENAME/array_location_calculation.rs" -OpMemberName %10 0 "x_axis" -OpMemberName %10 1 "y_axis" -OpMemberName %10 2 "z_axis" -OpName %10 "spirv_std::glam::core::storage::Columns3>" -OpMemberName %11 0 "0" -OpName %11 "spirv_std::glam::Mat3" -OpName %12 "array_location_calculation::main" +%10 = OpString "$OPSTRING_FILENAME/array_location_calculation.rs" +OpMemberName %11 0 "x_axis" +OpMemberName %11 1 "y_axis" +OpMemberName %11 2 "z_axis" +OpName %11 "spirv_std::glam::core::storage::Columns3>" +OpMemberName %12 0 "0" +OpName %12 "spirv_std::glam::Mat3" +OpName %13 "array_location_calculation::main" OpName %2 "one" OpName %3 "two" OpName %4 "three" @@ -25,20 +23,21 @@ OpName %5 "four" OpName %6 "five" OpName %7 "six" OpName %8 "seven" -OpMemberName %10 0 "x_axis" -OpMemberName %10 1 "y_axis" -OpMemberName %10 2 "z_axis" -OpMemberName %11 0 "0" -OpMemberName %11 0 "0" -OpMemberName %10 0 "x_axis" -OpMemberName %10 1 "y_axis" -OpMemberName %10 2 "z_axis" -OpDecorate %13 ArrayStride 4 +OpName %9 "eight" +OpMemberName %11 0 "x_axis" +OpMemberName %11 1 "y_axis" +OpMemberName %11 2 "z_axis" +OpMemberName %12 0 "0" +OpMemberName %12 0 "0" +OpMemberName %11 0 "x_axis" +OpMemberName %11 1 "y_axis" +OpMemberName %11 2 "z_axis" OpDecorate %14 ArrayStride 4 -OpMemberDecorate %10 0 Offset 0 -OpMemberDecorate %10 1 Offset 16 -OpMemberDecorate %10 2 Offset 32 +OpDecorate %15 ArrayStride 4 OpMemberDecorate %11 0 Offset 0 +OpMemberDecorate %11 1 Offset 16 +OpMemberDecorate %11 2 Offset 32 +OpMemberDecorate %12 0 Offset 0 OpDecorate %2 Location 0 OpDecorate %3 Location 7 OpDecorate %4 Location 10 @@ -46,33 +45,38 @@ OpDecorate %5 Location 13 OpDecorate %6 Location 15 OpDecorate %7 Location 16 OpDecorate %8 Location 17 -%15 = OpTypeVoid -%16 = OpTypeFloat 32 -%17 = OpTypeInt 32 0 -%18 = OpConstant %17 7 -%13 = OpTypeArray %16 %18 -%19 = OpConstant %17 3 -%14 = OpTypeArray %16 %19 -%20 = OpTypeVector %16 3 -%10 = OpTypeStruct %20 %20 %20 -%11 = OpTypeStruct %10 -%21 = OpTypeFloat 64 -%22 = OpTypeVector %21 3 -%23 = OpTypeInt 32 1 -%24 = OpTypeVector %23 4 -%25 = OpTypeFunction %15 %13 %14 %11 %22 %24 %16 %17 -%26 = OpTypeFunction %15 -%27 = OpTypePointer Input %13 -%2 = OpVariable %27 Input -%28 = OpTypePointer Input %14 -%3 = OpVariable %28 Input -%29 = OpTypePointer Input %11 -%4 = OpVariable %29 Input -%30 = OpTypePointer Input %22 -%5 = OpVariable %30 Input -%31 = OpTypePointer Input %24 -%6 = OpVariable %31 Input -%32 = OpTypePointer Input %16 -%7 = OpVariable %32 Input -%33 = OpTypePointer Input %17 -%8 = OpVariable %33 Input +OpDecorate %9 Location 20 +%16 = OpTypeVoid +%17 = OpTypeFloat 32 +%18 = OpTypeInt 32 0 +%19 = OpConstant %18 7 +%14 = OpTypeArray %17 %19 +%20 = OpConstant %18 3 +%15 = OpTypeArray %17 %20 +%21 = OpTypeVector %17 3 +%11 = OpTypeStruct %21 %21 %21 +%12 = OpTypeStruct %11 +%22 = OpTypeFloat 64 +%23 = OpTypeVector %22 3 +%24 = OpTypeInt 32 1 +%25 = OpTypeVector %24 4 +%26 = OpTypeVector %17 4 +%27 = OpTypeMatrix %26 3 +%28 = OpTypeFunction %16 %14 %15 %12 %23 %25 %17 %27 %18 +%29 = OpTypeFunction %16 +%30 = OpTypePointer Input %14 +%2 = OpVariable %30 Input +%31 = OpTypePointer Input %15 +%3 = OpVariable %31 Input +%32 = OpTypePointer Input %12 +%4 = OpVariable %32 Input +%33 = OpTypePointer Input %23 +%5 = OpVariable %33 Input +%34 = OpTypePointer Input %25 +%6 = OpVariable %34 Input +%35 = OpTypePointer Input %17 +%7 = OpVariable %35 Input +%36 = OpTypePointer Input %27 +%8 = OpVariable %36 Input +%37 = OpTypePointer Input %18 +%9 = OpVariable %37 Input From 31ab929586c5f31a4afc6e9425e9271eb7a2a2da Mon Sep 17 00:00:00 2001 From: Ashley Date: Sun, 14 Nov 2021 04:53:21 -0800 Subject: [PATCH 06/12] Update crates/rustc_codegen_spirv/src/codegen_cx/entry.rs --- crates/rustc_codegen_spirv/src/codegen_cx/entry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index a0d17a9396..6acbc0e103 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -563,7 +563,7 @@ impl<'tcx> CodegenCx<'tcx> { .lookup_const_u64(count) .expect("Array type has invalid count value") as u32 } - // Structs take up per component. + // Structs take up one location per field. SpirvType::Adt { field_types, .. } => { let mut size = 0; From d3521a5a88c3e8d85016c542440b1f48dcc7ecee Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sun, 14 Nov 2021 14:37:00 +0100 Subject: [PATCH 07/12] Normalise the stderr --- tests/ui/dis/array_location_calculation.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ui/dis/array_location_calculation.rs b/tests/ui/dis/array_location_calculation.rs index 2ff8bc56f3..fc4a56d0a9 100644 --- a/tests/ui/dis/array_location_calculation.rs +++ b/tests/ui/dis/array_location_calculation.rs @@ -1,5 +1,8 @@ // build-pass // compile-flags: -C llvm-args=--disassemble-globals +// normalize-stderr-test "OpCapability VulkanMemoryModel\n" -> "" +// normalize-stderr-test "OpExtension .SPV_KHR_vulkan_memory_model.\n" -> "" +// normalize-stderr-test "OpMemoryModel Logical Vulkan" -> "OpMemoryModel Logical Simple" use spirv_std::{ self as _, From 08d712a596855d426d29886b2464f6a0f39e2114 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Mon, 15 Nov 2021 10:46:01 +0100 Subject: [PATCH 08/12] Fix lint and avoid OpMemberName ordering shenanigans --- .../src/codegen_cx/entry.rs | 18 +++++++++++------- tests/ui/dis/array_location_calculation.rs | 1 + tests/ui/dis/array_location_calculation.stderr | 3 --- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 6acbc0e103..7127b41f8d 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -573,13 +573,17 @@ impl<'tcx> CodegenCx<'tcx> { size } - // 2 component vectors always take up 1 location. - SpirvType::Vector { count: 2, .. } => 1, - // 3 or 4 component vectors take up 2 locations if they have a 64-bit scalar type. - SpirvType::Vector { element, .. } => match self.lookup_type(element) { - SpirvType::Float(64) | SpirvType::Integer(64, _) => 2, - _ => 1, - }, + SpirvType::Vector { element, count } => { + // 3 or 4 component vectors take up 2 locations if they have a 64-bit scalar type. + if count > 2 { + match self.lookup_type(element) { + SpirvType::Float(64) | SpirvType::Integer(64, _) => 2, + _ => 1, + } + } else { + 1 + } + } SpirvType::Matrix { element, count } => count * self.location_size_of_type(element), _ => 1, } diff --git a/tests/ui/dis/array_location_calculation.rs b/tests/ui/dis/array_location_calculation.rs index fc4a56d0a9..ac5c22fd88 100644 --- a/tests/ui/dis/array_location_calculation.rs +++ b/tests/ui/dis/array_location_calculation.rs @@ -3,6 +3,7 @@ // normalize-stderr-test "OpCapability VulkanMemoryModel\n" -> "" // normalize-stderr-test "OpExtension .SPV_KHR_vulkan_memory_model.\n" -> "" // normalize-stderr-test "OpMemoryModel Logical Vulkan" -> "OpMemoryModel Logical Simple" +// normalize-stderr-test "OpMemberName %12 0 .0.\n" -> "" use spirv_std::{ self as _, diff --git a/tests/ui/dis/array_location_calculation.stderr b/tests/ui/dis/array_location_calculation.stderr index 8a5a05bf88..8ab0e9862d 100644 --- a/tests/ui/dis/array_location_calculation.stderr +++ b/tests/ui/dis/array_location_calculation.stderr @@ -13,7 +13,6 @@ OpMemberName %11 0 "x_axis" OpMemberName %11 1 "y_axis" OpMemberName %11 2 "z_axis" OpName %11 "spirv_std::glam::core::storage::Columns3>" -OpMemberName %12 0 "0" OpName %12 "spirv_std::glam::Mat3" OpName %13 "array_location_calculation::main" OpName %2 "one" @@ -27,8 +26,6 @@ OpName %9 "eight" OpMemberName %11 0 "x_axis" OpMemberName %11 1 "y_axis" OpMemberName %11 2 "z_axis" -OpMemberName %12 0 "0" -OpMemberName %12 0 "0" OpMemberName %11 0 "x_axis" OpMemberName %11 1 "y_axis" OpMemberName %11 2 "z_axis" From c3a29eaffe2c89f60b98672b399ad408afb8e356 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Wed, 24 Nov 2021 11:21:27 +0100 Subject: [PATCH 09/12] Rename function and handle arrays of structs --- crates/rustc_codegen_spirv/src/codegen_cx/entry.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 7127b41f8d..55facabd4e 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -535,7 +535,7 @@ impl<'tcx> CodegenCx<'tcx> { Decoration::Location, std::iter::once(Operand::LiteralInt32(*location)), ); - *location += self.location_size_of_type(value_spirv_type); + *location += self.location_slots_per_type(value_spirv_type); } // Emit the `OpVariable` with its *Result* ID set to `var`. @@ -555,20 +555,20 @@ impl<'tcx> CodegenCx<'tcx> { } } - fn location_size_of_type(&self, ty: Word) -> u32 { + fn location_slots_per_type(&self, ty: Word) -> u32 { match self.lookup_type(ty) { // Arrays take up multiple locations. - SpirvType::Array { count, .. } => { + SpirvType::Array { count, element } => { self.builder .lookup_const_u64(count) - .expect("Array type has invalid count value") as u32 + .expect("Array type has invalid count value") as u32 * self.location_slots_per_type(element) } // Structs take up one location per field. SpirvType::Adt { field_types, .. } => { let mut size = 0; for field_type in field_types { - size += self.location_size_of_type(field_type); + size += self.location_slots_per_type(field_type); } size @@ -584,7 +584,7 @@ impl<'tcx> CodegenCx<'tcx> { 1 } } - SpirvType::Matrix { element, count } => count * self.location_size_of_type(element), + SpirvType::Matrix { element, count } => count * self.location_slots_per_type(element), _ => 1, } } From 8548d95b62628b7bb8d88f187f98ae1083919b25 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Wed, 24 Nov 2021 11:36:12 +0100 Subject: [PATCH 10/12] Run cargo fmt --- crates/rustc_codegen_spirv/src/codegen_cx/entry.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 55facabd4e..ad7d50e0e4 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -561,7 +561,8 @@ impl<'tcx> CodegenCx<'tcx> { SpirvType::Array { count, element } => { self.builder .lookup_const_u64(count) - .expect("Array type has invalid count value") as u32 * self.location_slots_per_type(element) + .expect("Array type has invalid count value") as u32 + * self.location_slots_per_type(element) } // Structs take up one location per field. SpirvType::Adt { field_types, .. } => { From b39390f8fd4e4e210969398e24c722e8b9ba5674 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Mon, 13 Jun 2022 18:16:03 +0200 Subject: [PATCH 11/12] Rename -> location_count_of_type --- crates/rustc_codegen_spirv/src/codegen_cx/entry.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 043016c52a..b327d7554a 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -545,7 +545,7 @@ impl<'tcx> CodegenCx<'tcx> { Decoration::Location, std::iter::once(Operand::LiteralInt32(*location)), ); - *location += self.location_slots_per_type(value_spirv_type); + *location += self.location_count_of_type(value_spirv_type); } // Emit the `OpVariable` with its *Result* ID set to `var`. @@ -565,7 +565,7 @@ impl<'tcx> CodegenCx<'tcx> { } } - fn location_slots_per_type(&self, ty: Word) -> u32 { + fn location_count_of_type(&self, ty: Word) -> u32 { match self.lookup_type(ty) { // Arrays take up multiple locations. SpirvType::Array { count, element } => { From b720131fc145d1a3f1de482bd1850ed623a6c2c8 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Mon, 13 Jun 2022 18:20:02 +0200 Subject: [PATCH 12/12] Fix tests --- crates/rustc_codegen_spirv/src/codegen_cx/entry.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index b327d7554a..e8a62eebb6 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -572,14 +572,14 @@ impl<'tcx> CodegenCx<'tcx> { self.builder .lookup_const_u64(count) .expect("Array type has invalid count value") as u32 - * self.location_slots_per_type(element) + * self.location_count_of_type(element) } // Structs take up one location per field. SpirvType::Adt { field_types, .. } => { let mut size = 0; for field_type in field_types { - size += self.location_slots_per_type(field_type); + size += self.location_count_of_type(field_type); } size @@ -595,7 +595,7 @@ impl<'tcx> CodegenCx<'tcx> { 1 } } - SpirvType::Matrix { element, count } => count * self.location_slots_per_type(element), + SpirvType::Matrix { element, count } => count * self.location_count_of_type(element), _ => 1, } }