Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to lower llvm.fmuladd into mad from OpenCL extinst #824

Conversation

AlexeySachkov
Copy link
Contributor

This is a PR #793, which I had to re-create in order to enable Travis CI check (I hope it will work)

@@ -2231,6 +2231,22 @@ SPIRVValue *LLVMToSPIRV::transIntrinsicInst(IntrinsicInst *II,
// For llvm.fmuladd.* fusion is not guaranteed. If a fused multiply-add
// is required the corresponding llvm.fma.* intrinsic function should be
// used instead.
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no handling for fmuladd in checkTypeForSPIRVExtendedInstLowering, it will always return true. Should we extend checkTypeForSPIRVExtendedInstLowering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is necessary, because llvm.fmuladd.* is only defined for floating-point scalars, which perfectly matches requirements of fma extinst. We don't even need to check for vector length, because llvm.fmuladd.* is not defined for vectors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkTypeForSPIRVExtendedInstLowering takes II as a parameter. This function is based on switch on intrinsic id and there is no case for Intrinsic::fmuladd. We either should add case Intrinsic::fmuladd:, or don't call checkTypeForSPIRVExtendedInstLowering at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I will remove the call

include/LLVMSPIRVOpts.h Outdated Show resolved Hide resolved
lib/SPIRV/SPIRVWriter.cpp Outdated Show resolved Hide resolved
tools/llvm-spirv/llvm-spirv.cpp Outdated Show resolved Hide resolved
Comment on lines +205 to +209
static cl::opt<bool> SPIRVReplaceLLVMFmulAddWithOpenCLMad(
"spirv-replace-fmuladd-with-ocl-mad",
cl::desc("Allow replacement of llvm.fmuladd.* intrinsic with OpenCL mad "
"instruction from OpenCL extended instruction set"),
cl::init(true));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How likely is it that similar user-requested replacements will be added for other intrinsics? If such plans (or such bits of code) exist already, would a single "joint" option like -spirv-replace-intrinsics-with-ocl-ext be preferable?

Copy link
Contributor Author

@AlexeySachkov AlexeySachkov Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How likely is it that similar user-requested replacements will be added for other intrinsics?

This patch is more like a workaround for the lack of fine-grained fp-contraction support in SPIR-V spec, comparing it to OpenCL spec and LLVM IR representation. So, in general I wouldn't expect this to happen often, but still there might be a few more similar situations (right now I can't think of any)

If such plans (or such bits of code) exist already, would a single "joint" option like -spirv-replace-intrinsics-with-ocl-ext be preferable?

Since this is more like a workaround and I'm not aware of any similar things I guess it would be better to go with this particular option for fmuladd

@@ -202,6 +202,12 @@ static cl::opt<SPIRV::DebugInfoEIS> DebugEIS(
"extended instruction set. This version of SPIR-V debug "
"info format is compatible with the SPIRV-Tools")));

static cl::opt<bool> SPIRVReplaceLLVMFmulAddWithOpenCLMad(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to make it a bool-accepting option instead of a simple flag?
(I personally don't think it matters much, but just for the sake of consistency)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to make it a bool-accepting option instead of a simple flag?

No particular reason from command line interface point of view, but from technical point of view I need to take a more careful look at LLVM's CommandLine library to see if it is possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like boolOrDefault is what I'm looking for here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I was too quick with boolOrDefault: I don't need it, because I don't really care about whether the option was passed or not, but I care about its value.

Also, CommandLine library supports flags through boolean options, i.e. -spirv-replace-fmuladd-with-ocl-mad is equivalent to -spirv-replace-fmuladd-with-ocl-mad=true.

From my point of view the current way is good enough: most likely no one will ever use that switch and I don't want to overcomplicate the code just to get a -no-spirv-replace-fmuladd-with-ocl-mad alias (moreover, due to CommandLine library it would be really confusing to be able to use -no-spirv-replace-fmuladd-with-ocl-mad=false)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for expanding on this, SGTM.

lib/SPIRV/SPIRVWriter.cpp Outdated Show resolved Hide resolved
fadeeval and others added 7 commits December 8, 2020 18:57
Signed-off-by: Aleksander Fadeev <aleksander.fadeev@intel.com>
Signed-off-by: Aleksander Fadeev <aleksander.fadeev@intel.com>
Signed-off-by: Aleksander Fadeev <aleksander.fadeev@intel.com>
@AlexeySachkov AlexeySachkov force-pushed the private/asachkov/lower-fmuladd-to-mad branch from 05d38e0 to 514f5b8 Compare December 8, 2020 16:00
@AlexeySachkov AlexeySachkov merged commit 1cb18fd into KhronosGroup:master Dec 9, 2020
MrSidims pushed a commit to MrSidims/SPIRV-LLVM-Translator that referenced this pull request Jan 18, 2021
MrSidims pushed a commit to MrSidims/SPIRV-LLVM-Translator that referenced this pull request Jan 18, 2021
MrSidims pushed a commit to MrSidims/SPIRV-LLVM-Translator that referenced this pull request Jan 19, 2021
DmitryBushev pushed a commit to DmitryBushev/SPIRV-LLVM-Translator that referenced this pull request Sep 1, 2021
vmaksimo pushed a commit to vmaksimo/SPIRV-LLVM-Translator that referenced this pull request Sep 1, 2022
…ad from OpenCL.. 'master' -> 'xmain-web' (KhronosGroup#8)

  CONFLICT (content): Merge conflict in include/LLVMSPIRVOpts.h

  commit 1cb18fd
  Author: Alexey Sachkov <alexey.sachkov@intel.com>
  Date:   Wed Dec 9 17:45:33 2020 +0300

      Add possibility to lower llvm.fmuladd into mad from OpenCL extinst (KhronosGroup#824)

Change-Id: I725ce530db2dfe1c2f443b70f5d298b9a8238fc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants