This is an archive of the discontinued LLVM Phabricator instance.

[HLSL] Add abs library function
ClosedPublic

Authored by beanz on Aug 11 2022, 1:46 PM.

Details

Summary

This change exposes the abs library function for HLSL scalar types. Abs
is supported for all scalar, vector and matrix types. This patch only
adds a subset of scalar type support.

Fixes #57100 (https://llvm.org/pr57100)

The full documentation of the HLSL abs function is available here:
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-abs

Diff Detail

Event Timeline

beanz created this revision.Aug 11 2022, 1:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 1:46 PM
Herald added a subscriber: Anastasia. · View Herald Transcript
beanz requested review of this revision.Aug 11 2022, 1:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 1:46 PM
python3kgae added inline comments.Aug 11 2022, 1:52 PM
clang/lib/Headers/hlsl/hlsl_intrinsics.h
17

How to add 16bit abs?
Something like this?

#ifdef HLSL_ENABLE_16_BIT
attribute((clang_builtin_alias(builtin_fabsf16))) half abs(half In);
#endif

beanz updated this revision to Diff 451974.Aug 11 2022, 1:53 PM

Adding missing test coverage for 64-bit types

beanz updated this revision to Diff 454962.Aug 23 2022, 2:21 PM

Updating with a fix for half type parameters. This addresses the issue I filed here: https://github.com/llvm/llvm-project/issues/57100

The issue was caused by HLSL not implying options to allow half types to be used as parameters and return types.

bob80905 added inline comments.Aug 24 2022, 9:54 AM
clang/test/CodeGenHLSL/builtins/abs.hlsl
21

Is there a reason why we don't check the function parameter here, but we do check for function parameters when actually calling the builtin intrinsic on the next call line?

bob80905 added inline comments.Aug 24 2022, 10:00 AM
clang/include/clang/Driver/Options.td
6142

In Clang.cpp, you removed CmdArgs.push_back("-fallow-half-arguments-and-returns");
But here, you add an extra condition to imply fnative-half-arguments-and-returns
Wouldn't this change the behavior? or is it really alright for the definition below, fallow_half_arguments_and_returns , to be missing hlsl.KeyPath in the implication list?

beanz added inline comments.Aug 24 2022, 10:21 AM
clang/include/clang/Driver/Options.td
6142

The big issue with the change in Clang.cpp is that it only works if you invoke clang-dxc, not if you invoke clang directly with an hlsl source. That has the undesirable effect of making clang HLSL support only work through the clang-dxc driver, which isn't what we want.

There is also a subtle difference between native and non-native half arguments and returns, where non-native half arguments and returns are actually passed as larger float types. Which also isn't correct for HLSL.

This change follows how OpenCL works, and enables -fnative-half-arguments-and-returns whenever the HLSL language mode is enabled, and that option also implies -fhalf-arguments-and-returns.

The big functional change here is that you don't need to ever manually pass these flags to clang if your language is HLSL, so you can use any clang driver entry and get the correct behavior for the language.

clang/test/CodeGenHLSL/builtins/abs.hlsl
21

The check lines on the function declarations are really just to make sure that the next match occurs in the function body. The real test is to make sure the body gets generated correctly.

That said, matching the full mangled name has the same impact as matching the parameters too because the name mangling encodes the parameters too. The NN specifies the return and first argument parameters are doubles.

bogner accepted this revision.Aug 24 2022, 12:55 PM

LGTM. Make sure you update the description from saying that this doesn't handle half because of https://llvm.org/pr57100 to saying that it fixes the bug instead.

This revision is now accepted and ready to land.Aug 24 2022, 12:55 PM
beanz edited the summary of this revision. (Show Details)Aug 24 2022, 1:05 PM
This revision was automatically updated to reflect the committed changes.