-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[NFC] [SPIRV] Add SPIRVCombinerHelper and refactor pre legalizer combiner to use it #162735
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
//===-- SPIRVCombinerHelper.cpp ---------------------------------*- C++ -*-===// | ||
kmpeng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// | ||
// 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 "SPIRVCombinerHelper.h" | ||
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" | ||
#include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" | ||
#include "llvm/IR/IntrinsicsSPIRV.h" | ||
#include "llvm/Target/TargetMachine.h" | ||
|
||
using namespace llvm; | ||
using namespace MIPatternMatch; | ||
|
||
SPIRVCombinerHelper::SPIRVCombinerHelper( | ||
GISelChangeObserver &Observer, MachineIRBuilder &B, bool IsPreLegalize, | ||
GISelValueTracking *VT, MachineDominatorTree *MDT, const LegalizerInfo *LI, | ||
const SPIRVSubtarget &STI) | ||
: CombinerHelper(Observer, B, IsPreLegalize, VT, MDT, LI), STI(STI) {} | ||
|
||
/// This match is part of a combine that | ||
/// rewrites length(X - Y) to distance(X, Y) | ||
/// (f32 (g_intrinsic length | ||
/// (g_fsub (vXf32 X) (vXf32 Y)))) | ||
/// -> | ||
/// (f32 (g_intrinsic distance | ||
/// (vXf32 X) (vXf32 Y))) | ||
/// | ||
bool SPIRVCombinerHelper::matchLengthToDistance(MachineInstr &MI) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be moved to tablegen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as |
||
if (MI.getOpcode() != TargetOpcode::G_INTRINSIC || | ||
cast<GIntrinsic>(MI).getIntrinsicID() != Intrinsic::spv_length) | ||
return false; | ||
|
||
// First operand of MI is `G_INTRINSIC` so start at operand 2. | ||
Register SubReg = MI.getOperand(2).getReg(); | ||
MachineInstr *SubInstr = MRI.getVRegDef(SubReg); | ||
if (!SubInstr || SubInstr->getOpcode() != TargetOpcode::G_FSUB) | ||
kmpeng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return false; | ||
|
||
return true; | ||
} | ||
void SPIRVCombinerHelper::applySPIRVDistance(MachineInstr &MI) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to move this to tablegen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you point me to any examples of how to match a specific intrinsic in tablegen? I can't find a way to specify the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the intrinsic thing got fixed, if not it should be. |
||
// Extract the operands for X and Y from the match criteria. | ||
Register SubDestReg = MI.getOperand(2).getReg(); | ||
MachineInstr *SubInstr = MRI.getVRegDef(SubDestReg); | ||
Register SubOperand1 = SubInstr->getOperand(1).getReg(); | ||
Register SubOperand2 = SubInstr->getOperand(2).getReg(); | ||
Register ResultReg = MI.getOperand(0).getReg(); | ||
|
||
Builder.setInstrAndDebugLoc(MI); | ||
Builder.buildIntrinsic(Intrinsic::spv_distance, ResultReg) | ||
.addUse(SubOperand1) | ||
.addUse(SubOperand2); | ||
|
||
MI.eraseFromParent(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
kmpeng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
//===-- SPIRVCombinerHelper.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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// This contains common combine transformations that may be used in a combine | ||
/// pass. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVCOMBINERHELPER_H | ||
#define LLVM_LIB_TARGET_SPIRV_SPIRVCOMBINERHELPER_H | ||
|
||
#include "SPIRVSubtarget.h" | ||
#include "llvm/CodeGen/GlobalISel/CombinerHelper.h" | ||
|
||
namespace llvm { | ||
class SPIRVCombinerHelper : public CombinerHelper { | ||
protected: | ||
const SPIRVSubtarget &STI; | ||
|
||
public: | ||
using CombinerHelper::CombinerHelper; | ||
SPIRVCombinerHelper(GISelChangeObserver &Observer, MachineIRBuilder &B, | ||
bool IsPreLegalize, GISelValueTracking *VT, | ||
MachineDominatorTree *MDT, const LegalizerInfo *LI, | ||
const SPIRVSubtarget &STI); | ||
|
||
bool matchLengthToDistance(MachineInstr &MI) const; | ||
void applySPIRVDistance(MachineInstr &MI) const; | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVCOMBINERHELPER_H |
Uh oh!
There was an error while loading. Please reload this page.