KhronosGroup / KhronosGroup/SPIRV-LLVM-Translator

Expected fp_contract Behavior

Open
#557 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
LLVM
Stars
625
Forks
279
Avg merge
3d 5h
Merged PRs (30d)
34

Description

There have been several PRs recently to improve FP_CONTRACT behavior, for example:

* #509
* #521

I'm still not seeing the FP_CONTRACT behavior I'm expecting in some kernels though. It's possible that my expectations are wrong, but I figure I'll file this issue for documentation purposes if nothing else.

Here is my test kernel, which disables contractions:

```c
#pragma OPENCL FP_CONTRACT OFF
kernel void test(global float* f, float a, float b, float c)
{
f[0] = sin(a);
//f[1] = a * b + c;
}
```

When I'm compiling this kernel, I'm using:

```sh
$ clang -c -cl-std=CL1.2 -target spir64 -emit-llvm -Xclang -finclude-default-header -g0 -O3 no_contract.cl
$ llvm-spirv no_contract.bc -o no_contract.spv
$ spirv-dis no_contract.spv
```

With the kernel as-written above that only assigns to `f[0]`, I am not seeing `ContractionOff`, which I found a little surprising:

```
; SPIR-V
; Version: 1.0
; Generator: Khronos LLVM/SPIR-V Translator; 14
; Bound: 14
; Schema: 0
OpCapability Addresses
OpCapability Kernel
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %6 "test"
%13 = OpString "kernel_arg_type.test.float*,float,float,float,"
...
```

Un-commenting the assignment to `f[1]` does generate `ContractionOff`:

```
; SPIR-V
; Version: 1.0
; Generator: Khronos LLVM/SPIR-V Translator; 14
; Bound: 19
; Schema: 0
OpCapability Addresses
OpCapability Kernel
OpCapability Int64
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %6 "test"
OpExecutionMode %6 ContractionOff
%18 = OpString "kernel_arg_type.test.float*,float,float,float,"
...
```

My questions to start with are:

* Is this the expected behavior? Specifically, should `ContractionOff` **not** be in the SPIR-V module in the first case, even though the OpenCL C kernel disabled contractions?
* Let's say that the implementation of the `sin` function in the OpenCL extended instruction set includes multiplies and adds. Are these multiplies and adds subject to or independent from `ContractionsOff`?

Thanks!

Contributor guide

Open the contributing guide

Research direction

Start with the no_contract.cl kernel and reproduce the two clang, llvm-spirv, and spirv-dis command sequences shown in the issue. Compare the resulting SPIR-V and determine the expected ContractionOff behavior, including whether operations inside the sin extended instruction are covered; done means documenting clear answers to both questions.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers, documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.