This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Use functions instead of macros for const SIMD intrinsics
ClosedPublic

Authored by tlively on May 6 2021, 1:07 PM.

Details

Summary

To improve hygiene, consistency, and usability, it would be good to replace all
the macro intrinsics in wasm_simd128.h with functions. The reason for using
macros in the first place was to enforce the use of constants for some arguments
using _Static_assert with __builtin_constant_p. This commit switches to
using functions and uses the __diagnose_if__ attribute rather than
_Static_assert to enforce constantness.

The remaining macro intrinsics cannot be made into functions until the builtin
functions they are implemented with can be replaced with normal code patterns
because the builtin functions themselves require that their arguments are
constants.

This commit also fixes a bug with the const_splat intrinsics in which the f32x4
and f64x2 variants were incorrectly producing integer vectors.

Diff Detail

Event Timeline

tlively created this revision.May 6 2021, 1:07 PM
tlively requested review of this revision.May 6 2021, 1:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2021, 1:07 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tlively edited the summary of this revision. (Show Details)May 6 2021, 1:08 PM
aheejin accepted this revision.May 7 2021, 12:33 AM

To improve hygiene, consistency, and usability, it would be good to replace all
the macro intrinsics in wasm_simd128.h with functions. The reason for using
macros in the first place was to enforce the use of constants for some arguments
using _Static_assert with __builtin_constant_p. This commit switches to
using functions and uses the __diagnose_if__ attribute rather than
_Static_assert to enforce constantness.

So what prevented us from using functions when we were using _Static_assert and why is it now possible to use functions?

The remaining macro intrinsics cannot be made into functions until the builtin
functions they are implemented with can be replaced with normal code patterns
because the builtin functions themselves require that their arguments are
constants.

Why can't we also use __REQUIRE_CONSTANT there? Can't we call __REQUIRE_CONSTANT before we call builtins within intrinsic functions?

This revision is now accepted and ready to land.May 7 2021, 12:33 AM

So what prevented us from using functions when we were using _Static_assert and why is it now possible to use functions?

From inside a function, the parameter never looks like a constant because the function body does not know how the function will be used. Since the static_asserts would have been statements on the function body, they would not have been able to assert that the arguments to the function were constant. We were using macros to copy the static_asserts to each call site so they could see the actual arguments. In contrast, it looks like the diagnose_if attribute is evaluated separately for each call site and is able to see the actual arguments.

The remaining macro intrinsics cannot be made into functions until the builtin
functions they are implemented with can be replaced with normal code patterns
because the builtin functions themselves require that their arguments are
constants.

Why can't we also use __REQUIRE_CONSTANT there? Can't we call __REQUIRE_CONSTANT before we call builtins within intrinsic functions?

The new __REQUIRE_CONSTANT would be able to enforce that the intrinsic is only called with constant arguments, but that information is not propagated to the builtin call in the intrinsic's body. From the builtin's point of view inside the function body, its arguments are function parameters, so they may not be constant. One fix for this would have been to remove the part of the builtin definitions that requires the arguments to be constants, but that seemed more hacky than just keeping the original macros for a little bit longer.

This revision was landed with ongoing or failed builds.May 7 2021, 11:50 AM
This revision was automatically updated to reflect the committed changes.