Skip to content

[ESIMD] Break @llvm.fmuladd back into mul/add to satisfy VC BE. #5075

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

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions llvm/lib/SYCLLowerIR/LowerESIMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,39 @@ static Instruction *generateGenXCall(ExtractElementInst *EEI,
return Inst;
}

// Translates the following intrinsics:
// %res = call float @llvm.fmuladd.f32(float %a, float %b, float %c)
// %res = call double @llvm.fmuladd.f64(double %a, double %b, double %c)
// To
// %mul = fmul <type> %a, <type> %b
// %res = fadd <type> %mul, <type> %c
void translateFmuladd(CallInst *CI) {
assert(CI->getIntrinsicID() == Intrinsic::fmuladd);
IRBuilder<> Bld(CI);
auto *Mul = Bld.CreateFMul(CI->getOperand(0), CI->getOperand(1));
auto *Res = Bld.CreateFAdd(Mul, CI->getOperand(2));
CI->replaceAllUsesWith(Res);
}

// Translates an LLVM intrinsic to a form, digestable by the BE.
bool translateLLVMIntrinsic(CallInst *CI) {
Function *F = CI->getCalledFunction() ? CI->getCalledFunction() : nullptr;
assert(F && F->isIntrinsic());

switch (F->getIntrinsicID()) {
case Intrinsic::assume:
// no translation - it will be simply removed.
// TODO: make use of 'assume' info in the BE
break;
case Intrinsic::fmuladd:
translateFmuladd(CI);
break;
default:
return false; // "intrinsic wasn't translated, keep the original call"
}
return true; // "intrinsic has been translated, erase the original call"
}

/// Replaces the load \p LI of SPIRV global with corresponding call(s) of GenX
/// intrinsic(s). The users of \p LI may also be transformed if needed for
/// def/use type correctness.
Expand Down Expand Up @@ -1469,8 +1502,10 @@ size_t SYCLLowerESIMDPass::runOnFunction(Function &F,
Function *Callee = nullptr;
if (CI && (Callee = CI->getCalledFunction())) {
// TODO workaround for ESIMD BE until it starts supporting @llvm.assume
if (match(&I, PatternMatch::m_Intrinsic<Intrinsic::assume>())) {
ToErase.push_back(CI);
if (Callee->isIntrinsic()) {
if (translateLLVMIntrinsic(CI)) {
ToErase.push_back(CI);
}
continue;
}
StringRef Name = Callee->getName();
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/SYCLLowerIR/esimd_lower_llvm_intrin.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: opt -LowerESIMD -S < %s | FileCheck %s

; This test checks that LowerESIMD pass correctly lowers some llvm intrinsics
; which can't be handled by the VC BE.
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
target triple = "spir64-unknown-unknown"

declare float @llvm.fmuladd.f32(float %x, float %y, float %z)
declare double @llvm.fmuladd.f64(double %x, double %y, double %z)

define spir_func float @test_fmuladd_f32(float %x, float %y, float %z) {
%1 = call float @llvm.fmuladd.f32(float %x, float %y, float %z)
; CHECK: %[[A:[0-9a-zA-Z\._]+]] = fmul float %x, %y
; CHECK: %[[B:[0-9a-zA-Z\._]+]] = fadd float %[[A]], %z
ret float %1
; CHECK: ret float %[[B]]
}

define spir_func double @test_fmuladd_f64(double %x, double %y, double %z) {
%1 = call double @llvm.fmuladd.f64(double %x, double %y, double %z)
; CHECK: %[[A:[0-9a-zA-Z\._]+]] = fmul double %x, %y
; CHECK: %[[B:[0-9a-zA-Z\._]+]] = fadd double %[[A]], %z
ret double %1
; CHECK: ret double %[[B]]
}