Skip to content

Commit

Permalink
linker/test: enable DCE (adding new Exports where necessary).
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Dec 2, 2022
1 parent 2641f27 commit 5dacd94
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
16 changes: 15 additions & 1 deletion crates/rustc_codegen_spirv/src/linker/dce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! concept.
use rspirv::dr::{Function, Instruction, Module, Operand};
use rspirv::spirv::{Op, StorageClass, Word};
use rspirv::spirv::{Decoration, LinkageType, Op, StorageClass, Word};
use rustc_data_structures::fx::FxHashSet;

pub fn dce(module: &mut Module) {
Expand All @@ -19,9 +19,23 @@ pub fn dce(module: &mut Module) {

pub fn collect_roots(module: &Module) -> FxHashSet<Word> {
let mut rooted = FxHashSet::default();

for inst in &module.entry_points {
root(inst, &mut rooted);
}

// NOTE(eddyb) such "link exports" roots are only relevant when `Options`'s
// `keep_link_export`s field is used to request that `Export`s are left in
// (primarily for unit testing - see also its doc comment).
for inst in &module.annotations {
if inst.class.opcode == Op::Decorate
&& inst.operands[1].unwrap_decoration() == Decoration::LinkageAttributes
&& inst.operands[3].unwrap_linkage_type() == LinkageType::Export
{
root(inst, &mut rooted);
}
}

rooted
}

Expand Down
63 changes: 35 additions & 28 deletions crates/rustc_codegen_spirv/src/linker/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn assemble_and_link(binaries: &[&[u8]]) -> Result<Module, PrettyString> {
modules,
&Options {
compact_ids: true,
dce: false,
dce: true,
structurize: false,
emit_multiple_modules: false,
spirv_metadata: SpirvMetadata::None,
Expand Down Expand Up @@ -179,6 +179,8 @@ fn without_header_eq(mut result: Module, expected: &str) {

#[test]
fn standard() {
// FIXME(eddyb) the `Input` `OpVariable` is completely unused and after
// enabling DCE, it started being removed, is it necessary at all?
let a = assemble_spirv(
r#"OpCapability Linkage
OpMemoryModel Logical OpenCL
Expand All @@ -203,9 +205,8 @@ fn standard() {
OpMemoryModel Logical OpenCL
OpDecorate %1 LinkageAttributes "foo" Export
%2 = OpTypeFloat 32
%3 = OpVariable %2 Input
%4 = OpConstant %2 42.0
%1 = OpVariable %2 Uniform %4"#;
%3 = OpConstant %2 42.0
%1 = OpVariable %2 Uniform %3"#;

without_header_eq(result, expect);
}
Expand Down Expand Up @@ -386,6 +387,8 @@ fn decoration_mismatch() {

#[test]
fn func_ctrl() {
// FIXME(eddyb) the `Uniform` `OpVariable` is completely unused and after
// enabling DCE, it started being removed, is it necessary at all?
let a = assemble_spirv(
r#"OpCapability Linkage
OpMemoryModel Logical OpenCL
Expand Down Expand Up @@ -417,10 +420,8 @@ fn func_ctrl() {
OpDecorate %1 LinkageAttributes "foo" Export
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%4 = OpTypeFloat 32
%5 = OpVariable %4 Uniform
%1 = OpFunction %2 DontInline %3
%6 = OpLabel
%4 = OpLabel
OpReturn
OpFunctionEnd"#;

Expand All @@ -429,13 +430,15 @@ fn func_ctrl() {

#[test]
fn use_exported_func_param_attr() {
// HACK(eddyb) this keeps an otherwise-dead `OpFunction` alive w/ an `Export`.
let a = assemble_spirv(
r#"OpCapability Kernel
OpCapability Linkage
OpMemoryModel Logical OpenCL
OpDecorate %1 LinkageAttributes "foo" Import
OpDecorate %3 FuncParamAttr Zext
OpDecorate %4 FuncParamAttr Zext
OpDecorate %8 LinkageAttributes "HACK(eddyb) keep function alive" Export
%5 = OpTypeVoid
%6 = OpTypeInt 32 0
%7 = OpTypeFunction %5 %6
Expand Down Expand Up @@ -475,21 +478,22 @@ fn use_exported_func_param_attr() {
OpCapability Linkage
OpMemoryModel Logical OpenCL
OpDecorate %1 FuncParamAttr Zext
OpDecorate %2 LinkageAttributes "foo" Export
OpDecorate %3 FuncParamAttr Sext
%4 = OpTypeVoid
%5 = OpTypeInt 32 0
%6 = OpTypeFunction %4 %5
%7 = OpFunction %4 None %6
%1 = OpFunctionParameter %5
OpDecorate %2 LinkageAttributes "HACK(eddyb) keep function alive" Export
OpDecorate %3 LinkageAttributes "foo" Export
OpDecorate %4 FuncParamAttr Sext
%5 = OpTypeVoid
%6 = OpTypeInt 32 0
%7 = OpTypeFunction %5 %6
%2 = OpFunction %5 None %7
%1 = OpFunctionParameter %6
%8 = OpLabel
%9 = OpLoad %5 %1
%9 = OpLoad %6 %1
OpReturn
OpFunctionEnd
%2 = OpFunction %4 None %6
%3 = OpFunctionParameter %5
%3 = OpFunction %5 None %7
%4 = OpFunctionParameter %6
%10 = OpLabel
%11 = OpLoad %5 %3
%11 = OpLoad %6 %4
OpReturn
OpFunctionEnd"#;

Expand All @@ -498,6 +502,7 @@ fn use_exported_func_param_attr() {

#[test]
fn names_and_decorations() {
// HACK(eddyb) this keeps an otherwise-dead `OpFunction` alive w/ an `Export`.
let a = assemble_spirv(
r#"OpCapability Kernel
OpCapability Linkage
Expand All @@ -508,6 +513,7 @@ fn names_and_decorations() {
OpDecorate %3 Restrict
OpDecorate %4 Restrict
OpDecorate %4 NonWritable
OpDecorate %8 LinkageAttributes "HACK(eddyb) keep function alive" Export
%5 = OpTypeVoid
%6 = OpTypeInt 32 0
%9 = OpTypePointer Function %6
Expand Down Expand Up @@ -554,22 +560,23 @@ fn names_and_decorations() {
OpName %2 "param"
OpDecorate %3 Restrict
OpDecorate %3 NonWritable
OpDecorate %4 LinkageAttributes "HACK(eddyb) keep function alive" Export
OpDecorate %1 LinkageAttributes "foo" Export
OpDecorate %2 Restrict
%4 = OpTypeVoid
%5 = OpTypeInt 32 0
%6 = OpTypePointer Function %5
%7 = OpTypeFunction %4 %6
%8 = OpFunction %4 None %7
%3 = OpFunctionParameter %6
%5 = OpTypeVoid
%6 = OpTypeInt 32 0
%7 = OpTypePointer Function %6
%8 = OpTypeFunction %5 %7
%4 = OpFunction %5 None %8
%3 = OpFunctionParameter %7
%9 = OpLabel
%10 = OpLoad %5 %3
%10 = OpLoad %6 %3
OpReturn
OpFunctionEnd
%1 = OpFunction %4 None %7
%2 = OpFunctionParameter %6
%1 = OpFunction %5 None %8
%2 = OpFunctionParameter %7
%11 = OpLabel
%12 = OpLoad %5 %2
%12 = OpLoad %6 %2
OpReturn
OpFunctionEnd"#;

Expand Down

0 comments on commit 5dacd94

Please sign in to comment.