llvm-project/clang/lib/CodeGen
Michele Scandale b7d7c448df Fix `unsafe-fp-math` attribute emission.
The conditions for which Clang emits the `unsafe-fp-math` function
attribute has been modified as part of
`84a9ec2ff1ee97fd7e8ed988f5e7b197aab84a7`.
In the backend code generators `"unsafe-fp-math"="true"` enable floating
point contraction for the whole function.
The intent of the change in `84a9ec2ff1ee97fd7e8ed988f5e7b197aab84a7`
was to prevent backend code generators performing contractions when that
is not expected.
However the change is inaccurate and incomplete because it allows
`unsafe-fp-math` to be set also when only in-statement contraction is
allowed.

Consider the following example
```
float foo(float a, float b, float c) {
  float tmp = a * b;
  return tmp + c;
}
```
and compile it with the command line
```
clang -fno-math-errno -funsafe-math-optimizations -ffp-contract=on \
  -O2 -mavx512f -S -o -
```
The resulting assembly has a `vfmadd213ss` instruction which corresponds
to a fused multiply-add. From the user perspective there shouldn't be
any contraction because the multiplication and the addition are not in
the same statement.

The optimized IR is:
```
define float @test(float noundef %a, float noundef %b, float noundef %c) #0 {
  %mul = fmul reassoc nsz arcp afn float %b, %a
  %add = fadd reassoc nsz arcp afn float %mul, %c
  ret float %add
}

attributes #0 = {
  [...]
  "no-signed-zeros-fp-math"="true"
  "no-trapping-math"="true"
  [...]
  "unsafe-fp-math"="true"
}
```
The `"unsafe-fp-math"="true"` function attribute allows the backend code
generator to perform `(fadd (fmul a, b), c) -> (fmadd a, b, c)`.

In the current IR representation there is no way to determine the
statement boundaries from the original source code.
Because of this for in-statement only contraction the generated IR
doesn't have instructions with the `contract` fast-math flag and
`llvm.fmuladd` is being used to represent contractions opportunities
that occur within a single statement.
Therefore `"unsafe-fp-math"="true"` can only be emitted when contraction
across statements is allowed.

Moreover the change in `84a9ec2ff1ee97fd7e8ed988f5e7b197aab84a7` doesn't
take into account that the floating point math function attributes can
be refined during IR code generation of a function to handle the cases
where the floating point math options are modified within a compound
statement via pragmas (see `CGFPOptionsRAII`).
For consistency `unsafe-fp-math` needs to be disabled if the contraction
mode for any scope/operation is not `fast`.
Similarly for consistency reason the initialization of `UnsafeFPMath` of
in `TargetOptions` for the backend code generation should take into
account the contraction mode as well.

Reviewed By: zahiraam

Differential Revision: https://reviews.llvm.org/D136786
2022-11-14 20:40:57 -08:00
..
ABIInfo.h [clang][CodeGen] Factor out Swift ABI hooks (NFCI) 2022-08-08 00:23:23 +08:00
Address.h [CGOpenMPRuntime] Remove uses of deprecated Address constructor 2022-03-23 12:40:44 +01:00
BackendUtil.cpp Fix `unsafe-fp-math` attribute emission. 2022-11-14 20:40:57 -08:00
CGAtomic.cpp [clang-cl] Expose the /volatile:{iso,ms} choice via _ISO_VOLATILE 2022-08-23 14:29:52 +00:00
CGBlocks.cpp [clang] Qualify auto in range-based for loops (NFC) 2022-09-03 23:27:27 -07:00
CGBlocks.h [CodeGen] Treat ObjC `__unsafe_unretained` and class types as trivial 2022-01-11 11:18:24 -08:00
CGBuilder.h [clang] Add support for __builtin_memset_inline 2022-06-10 13:13:59 +00:00
CGBuiltin.cpp Add builtin_elementwise_sin and builtin_elementwise_cos 2022-11-10 23:30:27 -08:00
CGCUDANV.cpp Do not append terminating NUL to the binary string with embedded fatbin. 2022-10-17 15:39:39 -07:00
CGCUDARuntime.cpp
CGCUDARuntime.h Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
CGCXX.cpp
CGCXXABI.cpp [clang] CGCXXABI::EmitLoadOfMemberFunctionPointer - use castAs<> instead of getAs<> to avoid dereference of nullptr 2022-02-17 13:18:23 +00:00
CGCXXABI.h [clang] Add cc1 option -fctor-dtor-return-this 2022-10-03 14:28:06 -07:00
CGCall.cpp Fix `unsafe-fp-math` attribute emission. 2022-11-14 20:40:57 -08:00
CGCall.h [clang][CodeGen] Only include ABIInfo.h where required (NFC) 2022-07-22 10:45:02 -07:00
CGClass.cpp Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
CGCleanup.cpp [NFC][Alignment] Use MaybeAlign in CGCleanup/CGExpr 2022-06-14 10:56:36 +00:00
CGCleanup.h [clang] Remove unused forward declarations (NFC) 2022-01-08 11:56:40 -08:00
CGCoroutine.cpp [C++] [Coroutines] Prefer aligned (de)allocation for coroutines - 2022-09-22 11:28:29 +08:00
CGDebugInfo.cpp [clang][DebugInfo] Emit DISubprogram for extern functions with reserved names 2022-10-28 08:07:54 -07:00
CGDebugInfo.h [clang-cl] Implement /ZH: flag 2022-09-25 14:43:14 -04:00
CGDecl.cpp "Reapply "GH58368: Correct concept checking in a lambda defined in concept"" 2022-10-24 12:36:54 -07:00
CGDeclCXX.cpp Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
CGException.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
CGExpr.cpp [ObjC] avoid crashing when emitting synthesized getter/setter and ptrdiff_t is smaller than long 2022-11-10 02:10:30 -05:00
CGExprAgg.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
CGExprCXX.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
CGExprComplex.cpp [clang] use getCommonSugar in an assortment of places 2022-09-16 16:36:00 +02:00
CGExprConstant.cpp [Clang] Propagate const context info when emitting compound literal 2022-08-18 11:25:20 +01:00
CGExprScalar.cpp [clang][CodeGen] Consistently return nullptr Values for void builtins and scalar initalization 2022-10-24 21:41:13 +02:00
CGGPUBuiltin.cpp [OpenMP] Lower printf to __llvm_omp_vprintf 2021-11-10 15:30:56 +00:00
CGHLSLRuntime.cpp [HLSL] Remove unused frontend-generated ID 2022-10-21 12:41:09 -05:00
CGHLSLRuntime.h [HLSL] Remove unused frontend-generated ID 2022-10-21 12:41:09 -05:00
CGLoopInfo.cpp
CGLoopInfo.h
CGNonTrivialStruct.cpp Use llvm::append_range instead of push_back loops where applicable. NFCI. 2022-03-18 01:25:34 +01:00
CGObjC.cpp [ObjC] avoid crashing when emitting synthesized getter/setter and ptrdiff_t is smaller than long 2022-11-10 02:10:30 -05:00
CGObjCGNU.cpp [clang] Qualify auto in range-based for loops (NFC) 2022-09-03 23:27:27 -07:00
CGObjCMac.cpp [IR] Switch everything to use memory attribute 2022-11-04 10:21:38 +01:00
CGObjCRuntime.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
CGObjCRuntime.h [clang][CodeGen] Only include ABIInfo.h where required (NFC) 2022-07-22 10:45:02 -07:00
CGOpenCLRuntime.cpp [CodeGen] Avoid pointer element type access for blocks 2022-03-17 16:56:31 +01:00
CGOpenCLRuntime.h [CodeGen] Avoid pointer element type access for blocks 2022-03-17 16:56:31 +01:00
CGOpenMPRuntime.cpp Migrate getOrCreateInternalVariable from Clang to OMPIRBuilder. 2022-11-14 17:18:10 +00:00
CGOpenMPRuntime.h Migrate getOrCreateInternalVariable from Clang to OMPIRBuilder. 2022-11-14 17:18:10 +00:00
CGOpenMPRuntimeGPU.cpp [OpenMP][OMPIRBuilder] Migrate createOffloadEntriesAndInfoMetadata from clang to OpenMPIRBuilder 2022-11-03 10:27:44 -04:00
CGOpenMPRuntimeGPU.h [OpenMP][OMPIRBuilder] Migrate createOffloadEntriesAndInfoMetadata from clang to OpenMPIRBuilder 2022-11-03 10:27:44 -04:00
CGRecordLayout.h [Clang] Add helper method to determine if a nonvirtual base has an entry in the LLVM struct 2022-03-25 16:32:12 -04:00
CGRecordLayoutBuilder.cpp [clang] Fix bugprone argument comments (NFC) 2022-01-09 00:19:49 -08:00
CGStmt.cpp [OPENMP]Initial support for error directive. 2022-11-02 14:25:28 -07:00
CGStmtOpenMP.cpp [OPENMP]Initial support for error directive. 2022-11-02 14:25:28 -07:00
CGVTT.cpp [clang codegen] Add dso_local/hidden/etc. markings to VTT declarations 2022-06-24 09:58:31 -07:00
CGVTables.cpp [clang] Do not instrument the rtti_proxies under hwasan 2022-08-26 18:22:17 +00:00
CGVTables.h [clang] Do not instrument the rtti_proxies under hwasan 2022-08-26 18:22:17 +00:00
CGValue.h [CodeGen] Store element type in RValue 2021-12-17 09:05:59 +01:00
CMakeLists.txt [NFC] [HLSL] Move common metadata to LLVMFrontend 2022-10-14 13:40:04 -05:00
CodeGenABITypes.cpp
CodeGenAction.cpp clang: Fix unnecessary truncation of resource limit values 2022-11-11 16:38:51 -08:00
CodeGenFunction.cpp Fix `unsafe-fp-math` attribute emission. 2022-11-14 20:40:57 -08:00
CodeGenFunction.h [Clang][LoongArch] Implement __builtin_loongarch_dbar builtin 2022-11-10 17:27:44 +08:00
CodeGenModule.cpp [HLSL] CodeGen hlsl cbuffer/tbuffer. 2022-10-12 21:17:38 -07:00
CodeGenModule.h KCFI sanitizer 2022-08-24 22:41:38 +00:00
CodeGenPGO.cpp [InstrProf] Add the skipprofile attribute 2022-08-04 08:45:27 -07:00
CodeGenPGO.h
CodeGenTBAA.cpp Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
CodeGenTBAA.h [clang] Remove unused forward declarations (NFC) 2022-01-08 11:56:40 -08:00
CodeGenTypeCache.h Fix __attribute__((annotate("")) with non-zero globals AS 2021-08-26 10:09:40 +01:00
CodeGenTypes.cpp Properly print unnamed TagDecl objects in diagnostics 2022-10-14 08:18:28 -04:00
CodeGenTypes.h [clang] Properly cache member pointer LLVM types 2022-02-08 13:22:24 -08:00
ConstantEmitter.h [Clang] Propagate const context info when emitting compound literal 2022-08-18 11:25:20 +01:00
ConstantInitBuilder.cpp [clang][CodeGen] Switch a few placeholders from UndefValue to PoisonValue 2022-06-12 19:07:59 +01:00
CoverageMappingGen.cpp [Clang] Fix crash in coverage of if consteval. 2022-08-26 17:46:53 +02:00
CoverageMappingGen.h [Clang][CoverageMapping] Fix compile time explosions by adjusting only appropriated skipped ranges 2022-06-08 23:13:39 -07:00
EHScopeStack.h
ItaniumCXXABI.cpp [IR] Switch everything to use memory attribute 2022-11-04 10:21:38 +01:00
MacroPPCallbacks.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
MacroPPCallbacks.h [clang][lex] NFCI: Use FileEntryRef in PPCallbacks::InclusionDirective() 2022-04-14 10:46:12 +02:00
MicrosoftCXXABI.cpp Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
ModuleBuilder.cpp [CGDebugInfo] Access the current working directory from the `VFS` 2022-07-26 13:48:39 -07:00
ObjectFilePCHContainerOperations.cpp [clang] Qualify auto in range-based for loops (NFC) 2022-09-03 23:27:27 -07:00
PatternInit.cpp
PatternInit.h
README.txt
SanitizerMetadata.cpp Remove 'no_sanitize_memtag'. Add 'sanitize_memtag'. 2022-07-13 08:54:41 -07:00
SanitizerMetadata.h [clang] Remove unused forward declarations (NFC) 2022-07-24 20:51:06 -07:00
SwiftCallingConv.cpp [clang] Qualify auto in range-based for loops (NFC) 2022-09-03 23:27:27 -07:00
TargetInfo.cpp Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
TargetInfo.h [clang][CodeGen] Factor out Swift ABI hooks (NFCI) 2022-08-08 00:23:23 +08:00
VarBypassDetector.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
VarBypassDetector.h Use {DenseSet,SmallPtrSet}::contains (NFC) 2021-10-29 20:26:07 -07:00

README.txt

IRgen optimization opportunities.

//===---------------------------------------------------------------------===//

The common pattern of
--
short x; // or char, etc
(x == 10)
--
generates an zext/sext of x which can easily be avoided.

//===---------------------------------------------------------------------===//

Bitfields accesses can be shifted to simplify masking and sign
extension. For example, if the bitfield width is 8 and it is
appropriately aligned then is is a lot shorter to just load the char
directly.

//===---------------------------------------------------------------------===//

It may be worth avoiding creation of alloca's for formal arguments
for the common situation where the argument is never written to or has
its address taken. The idea would be to begin generating code by using
the argument directly and if its address is taken or it is stored to
then generate the alloca and patch up the existing code.

In theory, the same optimization could be a win for block local
variables as long as the declaration dominates all statements in the
block.

NOTE: The main case we care about this for is for -O0 -g compile time
performance, and in that scenario we will need to emit the alloca
anyway currently to emit proper debug info. So this is blocked by
being able to emit debug information which refers to an LLVM
temporary, not an alloca.

//===---------------------------------------------------------------------===//

We should try and avoid generating basic blocks which only contain
jumps. At -O0, this penalizes us all the way from IRgen (malloc &
instruction overhead), all the way down through code generation and
assembly time.

On 176.gcc:expr.ll, it looks like over 12% of basic blocks are just
direct branches!

//===---------------------------------------------------------------------===//