Skip to content

Commit

Permalink
[msl-out] Emit sign(i32) code inline
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 6, 2023
1 parent f6a2a91 commit e209bd9
Show file tree
Hide file tree
Showing 44 changed files with 42 additions and 185 deletions.
1 change: 0 additions & 1 deletion src/back/msl/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,4 @@ pub const RESERVED: &[&str] = &[
"clamped_lod_e",
super::writer::FREXP_FUNCTION,
super::writer::MODF_FUNCTION,
super::writer::ISIGN_FUNCTION,
];
53 changes: 39 additions & 14 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const RAY_QUERY_FUN_MAP_INTERSECTION: &str = "_map_intersection_type";

pub(crate) const MODF_FUNCTION: &str = "naga_modf";
pub(crate) const FREXP_FUNCTION: &str = "naga_frexp";
pub(crate) const ISIGN_FUNCTION: &str = "naga_isign";

/// Write the Metal name for a Naga numeric type: scalar, vector, or matrix.
///
Expand Down Expand Up @@ -1185,6 +1184,31 @@ impl<W: Write> Writer<W> {
Ok(())
}

/// Emit code for the sign(i32) expression.
///
fn put_isign(
&mut self,
arg: Handle<crate::Expression>,
context: &ExpressionContext,
) -> BackendResult {
write!(self.out, "{NAMESPACE}::select({NAMESPACE}::select(")?;
match context.resolve_type(arg) {
&crate::TypeInner::Vector { size, .. } => {
let size = back::vector_size_str(size);
write!(self.out, "int{size}(-1), int{size}(1)")?;
}
_ => {
write!(self.out, "-1, 1")?;
}
}
write!(self.out, ", (")?;
self.put_expression(arg, context, true)?;
write!(self.out, " > 0)), 0, (")?;
self.put_expression(arg, context, true)?;
write!(self.out, " == 0))")?;
Ok(())
}

fn put_const_expression(
&mut self,
expr_handle: Handle<crate::Expression>,
Expand Down Expand Up @@ -1716,7 +1740,9 @@ impl<W: Write> Writer<W> {
Mf::Refract => "refract",
// computational
Mf::Sign => match arg_type.scalar_kind() {
Some(crate::ScalarKind::Sint) => ISIGN_FUNCTION,
Some(crate::ScalarKind::Sint) => {
return self.put_isign(arg, context);
}
_ => "sign",
},
Mf::Fma => "fma",
Expand Down Expand Up @@ -1821,7 +1847,7 @@ impl<W: Write> Writer<W> {
write!(self.out, "((")?;
self.put_expression(arg, context, false)?;
write!(self.out, ") * 57.295779513082322865)")?;
} else if fun == Mf::Modf || fun == Mf::Frexp || fun_name == ISIGN_FUNCTION {
} else if fun == Mf::Modf || fun == Mf::Frexp {
write!(self.out, "{fun_name}")?;
self.put_call_parameters(iter::once(arg), context)?;
} else {
Expand Down Expand Up @@ -2428,6 +2454,16 @@ impl<W: Write> Writer<W> {
crate::MathFunction::FindMsb => {
self.need_bake_expressions.insert(arg);
}
crate::MathFunction::Sign => {
// WGSL's `sign` function works also on signed ints, but Metal's only
// works on floating-point vectors, so we emit inline code for
// integer `sign` calls. But that code uses each argument 2
// times, so to avoid duplicated evaluation, we must bake the argument.
let inner = context.resolve_type(expr_handle);
if inner.scalar_kind() == Some(crate::ScalarKind::Sint) {
self.need_bake_expressions.insert(arg);
}
}
_ => {}
}
}
Expand Down Expand Up @@ -3096,7 +3132,6 @@ impl<W: Write> Writer<W> {

self.write_type_defs(module)?;
self.write_global_constants(module)?;
self.write_polyfills()?;
self.write_functions(module, info, options, pipeline_options)
}

Expand Down Expand Up @@ -4109,16 +4144,6 @@ impl<W: Write> Writer<W> {
Ok(info)
}

fn write_polyfills(&mut self) -> Result<(), Error> {
writeln!(
self.out,
"\ntemplate <typename T> inline T {ISIGN_FUNCTION}(T arg) {{
return {NAMESPACE}::select({NAMESPACE}::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}}"
)?;
Ok(())
}

fn write_barrier(&mut self, flags: crate::Barrier, level: back::Level) -> BackendResult {
// Note: OR-ring bitflags requires `__HAVE_MEMFLAG_OPERATORS__`,
// so we try to avoid it here.
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/access.msl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ struct type_26 {
metal::float4 inner[2];
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

void test_matrix_within_struct_accesses(
constant Baz& baz
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/array-in-ctor.msl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ struct Ah {
type_1 inner;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

kernel void cs_main(
device Ah const& ah [[user(fake0)]]
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/array-in-function-return-type.msl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ struct type_1 {
float inner[2];
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

type_1 ret_array(
) {
return type_1 {1.0, 2.0};
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/atomicOps.msl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ struct Struct {
type_2 atomic_arr;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct cs_mainInput {
};
kernel void cs_main(
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/binding-arrays.msl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ struct FragmentIn {
uint index;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct main_Input {
uint index [[user(loc0), flat]];
};
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bitcast.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

kernel void main_(
) {
metal::int2 i2_ = {};
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bits.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

kernel void main_(
) {
int i = {};
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/boids.msl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ struct Particles {
};
constant uint NUM_PARTICLES = 1500u;

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct main_Input {
};
kernel void main_(
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bounds-check-image-restrict.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

metal::float4 test_textureLoad_1d(
int coords,
int level,
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bounds-check-image-rzsw.msl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ struct DefaultConstructible {
};


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

metal::float4 test_textureLoad_1d(
int coords,
int level,
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bounds-check-restrict.msl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ struct Globals {
type_4 d;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

float index_array(
int i,
device Globals const& globals,
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bounds-check-zero-atomic.msl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ struct Globals {
type_2 c;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

uint fetch_add_atomic(
device Globals& globals,
constant _mslBufferSizes& _buffer_sizes
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/bounds-check-zero.msl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ struct Globals {
type_4 d;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

float index_array(
int i,
device Globals const& globals,
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/break-if.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

void breakIfEmpty(
) {
bool loop_init = true;
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/collatz.msl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ struct PrimeIndices {
type_1 data;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

uint collatz_iterations(
uint n_base
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/constructors.msl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ constant type_10 cz6_ = type_10 {};
constant Foo cz7_ = Foo {};
constant type_11 cp3_ = type_11 {0, 1, 2, 3};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

kernel void main_(
) {
Foo foo = {};
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/control-flow.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

void switch_default_break(
int i
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/do-while.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

void fb1_(
thread bool& cond
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/dualsource.msl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ struct FragmentOutput {
metal::float4 mask;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct main_Input {
};
struct main_Output {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/empty-global-name.msl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ struct type_1 {
int member;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

void function(
device type_1& unnamed
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/empty.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

kernel void main_(
) {
return;
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/extra.msl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ struct FragmentIn {
uint primitive_index;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct main_Input {
metal::float4 color [[user(loc0), center_perspective]];
};
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/fragment-output.msl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ struct FragmentOutputVec2Scalar {
uint scalaru;
};

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct main_vec4vec3_Output {
metal::float4 vec4f [[color(0)]];
metal::int4 vec4i [[color(1)]];
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/functions.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

metal::float2 test_fma(
) {
metal::float2 a = metal::float2(2.0, 2.0);
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/globals.msl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ struct type_15 {
};
constant bool Foo_1 = true;

template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

void test_msl_packed_vec3_as_arg(
metal::float3 arg
) {
Expand Down
4 changes: 0 additions & 4 deletions tests/out/msl/image.msl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
using metal::uint;


template <typename T> inline T naga_isign(T arg) {
return metal::select(metal::select(T(-1), T(1), (arg > 0)), 0, (arg == 0));
}

struct main_Input {
};
kernel void main_(
Expand Down
Loading

0 comments on commit e209bd9

Please sign in to comment.