-
Notifications
You must be signed in to change notification settings - Fork 14.3k
PreISelIntrinsicLowering: Lower llvm.exp/llvm.exp2 to a loop if scalable vec arg #117568
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bac7223
PreISelIntrinsicLowering: Lower llvm.exp/llvm.exp2 to a loop if scala…
steplong 7c3ce2a
Move helper function to TargetLoweringBase
steplong f588c72
Return ISD::DELETED_NODE in IntrinsicIDToISD()'s default case
steplong e7ab0ec
Add comment about returning ISD::DELETED_NODE by default
steplong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
llvm/include/llvm/Transforms/Utils/LowerVectorIntrinsics.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===- llvm/Transforms/Utils/LowerVectorIntrinsics.h ------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Lower intrinsics with a scalable vector arg to loops. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_UTILS_LOWERVECTORINTRINSICS_H | ||
#define LLVM_TRANSFORMS_UTILS_LOWERVECTORINTRINSICS_H | ||
|
||
#include <cstdint> | ||
#include <optional> | ||
|
||
namespace llvm { | ||
|
||
class CallInst; | ||
class Module; | ||
|
||
/// Lower \p CI as a loop. \p CI is a unary intrinsic with a vector argument and | ||
/// is deleted and replaced with a loop. | ||
bool lowerUnaryVectorIntrinsicAsLoop(Module &M, CallInst *CI); | ||
|
||
} // namespace llvm | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//===- LowerVectorIntrinsics.cpp ------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Utils/LowerVectorIntrinsics.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/IntrinsicInst.h" | ||
#include "llvm/Support/Debug.h" | ||
|
||
#define DEBUG_TYPE "lower-vector-intrinsics" | ||
|
||
using namespace llvm; | ||
|
||
bool llvm::lowerUnaryVectorIntrinsicAsLoop(Module &M, CallInst *CI) { | ||
Type *ArgTy = CI->getArgOperand(0)->getType(); | ||
VectorType *VecTy = cast<VectorType>(ArgTy); | ||
|
||
BasicBlock *PreLoopBB = CI->getParent(); | ||
BasicBlock *PostLoopBB = nullptr; | ||
Function *ParentFunc = PreLoopBB->getParent(); | ||
LLVMContext &Ctx = PreLoopBB->getContext(); | ||
|
||
PostLoopBB = PreLoopBB->splitBasicBlock(CI); | ||
BasicBlock *LoopBB = BasicBlock::Create(Ctx, "", ParentFunc, PostLoopBB); | ||
PreLoopBB->getTerminator()->setSuccessor(0, LoopBB); | ||
|
||
// Loop preheader | ||
IRBuilder<> PreLoopBuilder(PreLoopBB->getTerminator()); | ||
Value *LoopEnd = nullptr; | ||
if (auto *ScalableVecTy = dyn_cast<ScalableVectorType>(VecTy)) { | ||
Value *VScale = PreLoopBuilder.CreateVScale( | ||
ConstantInt::get(PreLoopBuilder.getInt64Ty(), 1)); | ||
Value *N = ConstantInt::get(PreLoopBuilder.getInt64Ty(), | ||
ScalableVecTy->getMinNumElements()); | ||
LoopEnd = PreLoopBuilder.CreateMul(VScale, N); | ||
} else { | ||
FixedVectorType *FixedVecTy = cast<FixedVectorType>(VecTy); | ||
LoopEnd = ConstantInt::get(PreLoopBuilder.getInt64Ty(), | ||
FixedVecTy->getNumElements()); | ||
} | ||
|
||
// Loop body | ||
IRBuilder<> LoopBuilder(LoopBB); | ||
Type *Int64Ty = LoopBuilder.getInt64Ty(); | ||
|
||
PHINode *LoopIndex = LoopBuilder.CreatePHI(Int64Ty, 2); | ||
LoopIndex->addIncoming(ConstantInt::get(Int64Ty, 0U), PreLoopBB); | ||
PHINode *Vec = LoopBuilder.CreatePHI(VecTy, 2); | ||
Vec->addIncoming(CI->getArgOperand(0), PreLoopBB); | ||
|
||
Value *Elem = LoopBuilder.CreateExtractElement(Vec, LoopIndex); | ||
Function *Exp = Intrinsic::getOrInsertDeclaration(&M, CI->getIntrinsicID(), | ||
VecTy->getElementType()); | ||
Value *Res = LoopBuilder.CreateCall(Exp, Elem); | ||
Value *NewVec = LoopBuilder.CreateInsertElement(Vec, Res, LoopIndex); | ||
Vec->addIncoming(NewVec, LoopBB); | ||
|
||
Value *One = ConstantInt::get(Int64Ty, 1U); | ||
Value *NextLoopIndex = LoopBuilder.CreateAdd(LoopIndex, One); | ||
LoopIndex->addIncoming(NextLoopIndex, LoopBB); | ||
|
||
Value *ExitCond = | ||
LoopBuilder.CreateICmp(CmpInst::ICMP_EQ, NextLoopIndex, LoopEnd); | ||
LoopBuilder.CreateCondBr(ExitCond, PostLoopBB, LoopBB); | ||
|
||
CI->replaceAllUsesWith(NewVec); | ||
CI->eraseFromParent(); | ||
return true; | ||
} |
43 changes: 43 additions & 0 deletions
43
llvm/test/Transforms/PreISelIntrinsicLowering/AArch64/expand-exp.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt -passes=pre-isel-intrinsic-lowering -S < %s | FileCheck %s | ||
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32" | ||
target triple = "aarch64" | ||
|
||
define <vscale x 4 x float> @scalable_vec_exp(<vscale x 4 x float> %input) { | ||
; CHECK-LABEL: define <vscale x 4 x float> @scalable_vec_exp( | ||
; CHECK-SAME: <vscale x 4 x float> [[INPUT:%.*]]) { | ||
; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.vscale.i64() | ||
; CHECK-NEXT: [[TMP2:%.*]] = mul i64 [[TMP1]], 4 | ||
; CHECK-NEXT: br label %[[BB3:.*]] | ||
; CHECK: [[BB3]]: | ||
; CHECK-NEXT: [[TMP4:%.*]] = phi i64 [ 0, [[TMP0:%.*]] ], [ [[TMP9:%.*]], %[[BB3]] ] | ||
; CHECK-NEXT: [[TMP5:%.*]] = phi <vscale x 4 x float> [ [[INPUT]], [[TMP0]] ], [ [[TMP8:%.*]], %[[BB3]] ] | ||
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <vscale x 4 x float> [[TMP5]], i64 [[TMP4]] | ||
; CHECK-NEXT: [[TMP7:%.*]] = call float @llvm.exp.f32(float [[TMP6]]) | ||
; CHECK-NEXT: [[TMP8]] = insertelement <vscale x 4 x float> [[TMP5]], float [[TMP7]], i64 [[TMP4]] | ||
; CHECK-NEXT: [[TMP9]] = add i64 [[TMP4]], 1 | ||
; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i64 [[TMP9]], [[TMP2]] | ||
; CHECK-NEXT: br i1 [[TMP10]], label %[[BB11:.*]], label %[[BB3]] | ||
; CHECK: [[BB11]]: | ||
; CHECK-NEXT: ret <vscale x 4 x float> [[TMP8]] | ||
; | ||
%output = call <vscale x 4 x float> @llvm.exp.nxv4f32(<vscale x 4 x float> %input) | ||
ret <vscale x 4 x float> %output | ||
} | ||
|
||
define <4 x float> @fixed_vec_exp(<4 x float> %input) { | ||
; CHECK-LABEL: define <4 x float> @fixed_vec_exp( | ||
; CHECK-SAME: <4 x float> [[INPUT:%.*]]) { | ||
; CHECK-NEXT: [[OUTPUT:%.*]] = call <4 x float> @llvm.exp.v4f32(<4 x float> [[INPUT]]) | ||
; CHECK-NEXT: ret <4 x float> [[OUTPUT]] | ||
; | ||
%output = call <4 x float> @llvm.exp.v4f32(<4 x float> %input) | ||
ret <4 x float> %output | ||
} | ||
|
||
declare <4 x float> @llvm.exp.v4f32(<4 x float>) #0 | ||
declare <vscale x 4 x float> @llvm.exp.nxv4f32(<vscale x 4 x float>) #0 | ||
|
||
steplong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; CHECK: attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } | ||
; CHECK-NEXT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) } | ||
attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } |
2 changes: 2 additions & 0 deletions
2
llvm/test/Transforms/PreISelIntrinsicLowering/AArch64/lit.local.cfg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
if not "AArch64" in config.root.targets: | ||
config.unsupported = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.