[PATCH] D100118: [clang] RFC Support new arithmetic __fence builtin to control floating point optiization
Melanie Blower via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 8 08:45:34 PDT 2021
mibintc created this revision.
mibintc added a reviewer: kpn.
Herald added a subscriber: jfb.
mibintc requested review of this revision.
Herald added projects: clang, LLVM.
Herald added a subscriber: llvm-commits.
This is a proposal to add a new clang builtin, __arithmetic_fence. The purpose of the builtin is to provide the user fine control, at the expression level, over floating point optimization when -ffast-math (-ffp-model=fast) is enabled. We are also proposing a new clang builtin that provides access to this intrinsic, as well as a new clang command line option `-fprotect-parens` that will be implemented using this intrinsic.
This is the clang patch corresponding to https://reviews.llvm.org/D99675 which proposes a new llvm intrinsic llvm.arith.fence
Note: Preliminary patch, not ready for code review, doesn't yet run end-to-end with the llvm patch, and the logic for the new option is not yet written.
Rationale
---------
Some expression transformations that are mathematically correct, such as reassociation and distribution, may be incorrect when dealing with finite precision floating point. For example, these two expressions,
(a + b) + c
a + (b + c)
are equivalent mathematically in integer arithmetic, but not in floating point. In some floating point (FP) models, the compiler is allowed to make these value-unsafe transformations for performance reasons, even when the programmer uses parentheses explicitly. But the compiler must always honor the parentheses implied by __arithmetic_fence, regardless of the FP model settings.
Under `–ffp-model=fast`, __arithmetic_fence provides a way to partially enforce ordering in an FP expression.
| Original expression | Transformed expression | Permitted? |
| ----------------------------- | ---------------------- | ---------- |
| (a + b) + c | a + (b + c) | Yes! |
| __arithmetic_fence(a + b) + c | a + (b + c) | No! |
|
NOTE: The __arithmetic_fence serves no purpose in value-safe FP modes like `–ffp-model=precise`: FP expressions are already strictly ordered.
Motivation:
-----------
- The new clang builtin provides clang compatibility with the Intel C++ compiler builtin `__fence` which has similar semantics, and likewise enables implementation of the option `-fprotect-parens`.
- The new builtin provides the clang programmer control over floating point optimizations at the expression level.
- The gnu fortran compiler, gfortran, likewise supports `-fprotect-parens`
Requirements for __arithmetic_fence:
------------------------------------
- There is one operand. The operand type must be scalar floating point, complex floating point or vector thereof.
- The return type is the same as the operand type.
- The return value is equivalent to the operand.
- The invocation of __arithmetic_fence is not a C/C++ constant expression, even if the operands are constant.
New option `-fprotect-parens`
-----------------------------
- Determines whether the optimizer honors parentheses when floating-point expressions are evaluated
- Option defaults to `fno-protect-parens`
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D100118
Files:
clang/include/clang/Basic/Builtins.def
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Sema/Sema.h
clang/lib/AST/ExprConstant.cpp
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Sema/SemaChecking.cpp
clang/test/CodeGen/arithmetic-fence-builtin.c
clang/test/Sema/arithmetic-fence-builtin.c
llvm/include/llvm/IR/IRBuilder.h
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100118.336125.patch
Type: text/x-patch
Size: 9372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210408/5fc9eb4e/attachment.bin>
More information about the cfe-commits
mailing list