Skip to content

Commit ed51a1c

Browse files
dmitryryinteligcbot
authored andcommitted
Utils to create read/write_variable_region
Utils to create read/write_variable_region intrinsics were added.
1 parent 9f0d47d commit ed51a1c

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

IGC/VectorCompiler/include/vc/Utils/GenX/IRBuilder.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ SPDX-License-Identifier: MIT
99
#ifndef VC_UTILS_GENX_IRBUILDER_H
1010
#define VC_UTILS_GENX_IRBUILDER_H
1111

12+
#include "vc/Utils/GenX/Region.h"
13+
1214
#include "Probe/Assertion.h"
1315

16+
#include <llvm/ADT/Twine.h>
1417
#include <llvm/IR/IRBuilder.h>
1518

1619
namespace llvm {
1720
class Value;
21+
class CallInst;
1822
} // namespace llvm
1923

2024
namespace vc {
@@ -23,6 +27,35 @@ namespace vc {
2327
// The produced IR is valid only for target that have payload in memory (PIM).
2428
llvm::Value *getGroupThreadIDForPIM(llvm::IRBuilder<> &IRB);
2529

30+
// Produces IR that reads a region \p R from a vISA variable \p Variable.
31+
llvm::CallInst *
32+
createReadVariableRegion(llvm::GlobalVariable &Variable,
33+
const llvm::CMRegion &R, llvm::IRBuilder<> &IRB,
34+
const llvm::Twine &Name = "rd.var.rgn");
35+
36+
// Produces IR that reads a whole vISA variable \p Variable.
37+
inline llvm::CallInst *
38+
createReadVariableRegion(llvm::GlobalVariable &Variable, llvm::IRBuilder<> &IRB,
39+
const llvm::Twine &Name = "rd.var.rgn") {
40+
return createReadVariableRegion(Variable, {Variable.getValueType()}, IRB,
41+
Name);
42+
}
43+
44+
// Produces IR that writes \p Input to a region \p R of a vISA variable
45+
// \p Variable.
46+
llvm::CallInst *createWriteVariableRegion(llvm::GlobalVariable &Variable,
47+
llvm::Value &Input,
48+
const llvm::CMRegion &R,
49+
llvm::IRBuilder<> &IRB);
50+
51+
// Produces IR that writes \p Input to a whole vISA variable \p Variable.
52+
inline llvm::CallInst *createWriteVariableRegion(llvm::GlobalVariable &Variable,
53+
llvm::Value &Input,
54+
llvm::IRBuilder<> &IRB) {
55+
return createWriteVariableRegion(Variable, Input, {Variable.getValueType()},
56+
IRB);
57+
}
58+
2659
} // namespace vc
2760

2861
#endif // VC_UTILS_GENX_IRBUILDER_H

IGC/VectorCompiler/include/vc/Utils/General/Types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ llvm::Type &fixDegenerateVectorType(llvm::Type &Ty);
6767
// To some extend this function is the opposite of vc::fixDegenerateVectorType.
6868
IGCLLVM::FixedVectorType &getVectorType(llvm::Type &Ty);
6969

70+
// Takes \p OrigTy, replaces its scalar type with \p ScalarTy. The produced type
71+
// is returned.
72+
// \p OrigTy must be an integer, floating point, pointer type or vector of those
73+
// types. \p ScalarTy must be an integer, floating point or pointer type.
74+
// Examples:
75+
// (<3 x i32>, i1) -> <3 x i1>
76+
// (float, i16) -> i16
77+
// (<4 x double>, double) -> <4 x double>
78+
llvm::Type *setScalarType(llvm::Type &OrigTy, llvm::Type &ScalarTy);
79+
7080
} // namespace vc
7181

7282
#endif // VC_UTILS_GENERAL_TYPES_H

IGC/VectorCompiler/lib/Utils/GenX/IRBuilder.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ SPDX-License-Identifier: MIT
88

99
#include "vc/Utils/GenX/IRBuilder.h"
1010

11+
#include "vc/InternalIntrinsics/InternalIntrinsics.h"
12+
#include "vc/Utils/GenX/Intrinsics.h"
1113
#include "vc/Utils/GenX/TypeSize.h"
14+
#include "vc/Utils/General/Types.h"
1215

1316
#include "Probe/Assertion.h"
1417
#include "llvmWrapper/IR/DerivedTypes.h"
@@ -38,3 +41,34 @@ Value *vc::getGroupThreadIDForPIM(IRBuilder<> &IRB) {
3841
"r0.bytes");
3942
return IRB.CreateExtractElement(R0InBytes, TIDIndexInBytes, "r0.tid");
4043
}
44+
45+
CallInst *vc::createReadVariableRegion(GlobalVariable &Variable,
46+
const CMRegion &R, IRBuilder<> &IRB,
47+
const Twine &Name) {
48+
IGC_ASSERT_MESSAGE(R.ElementTy == Variable.getValueType()->getScalarType(),
49+
"wrong arguments: region and variable types don't match");
50+
Value *Args[] = {&Variable, IRB.getInt32(R.VStride), IRB.getInt32(R.Width),
51+
IRB.getInt32(R.Stride),
52+
IRB.getInt32(R.getOffsetInElements())};
53+
Function *Decl = vc::getInternalDeclarationForIdFromArgs(
54+
R.getRegionType(), Args, vc::InternalIntrinsic::read_variable_region,
55+
*IRB.GetInsertPoint()->getModule());
56+
return IRB.CreateCall(Decl, Args, Name);
57+
}
58+
59+
CallInst *vc::createWriteVariableRegion(GlobalVariable &Variable, Value &Input,
60+
const CMRegion &R, IRBuilder<> &IRB) {
61+
IGC_ASSERT_MESSAGE(R.ElementTy == Variable.getValueType()->getScalarType(),
62+
"wrong arguments: region and variable types don't match");
63+
IGC_ASSERT_MESSAGE(R.getRegionType() == Input.getType(),
64+
"wrong arguments: region and input types don't match");
65+
IGC_ASSERT_MESSAGE(R.is1D(), "wrong arguments: region must be 1D");
66+
auto *Mask = Constant::getAllOnesValue(
67+
vc::setScalarType(*Input.getType(), *IRB.getInt1Ty()));
68+
Value *Args[] = {&Variable, &Input, IRB.getInt32(R.getDstStride()),
69+
IRB.getInt32(R.getOffsetInElements()), Mask};
70+
Function *Decl = vc::getInternalDeclarationForIdFromArgs(
71+
IRB.getVoidTy(), Args, vc::InternalIntrinsic::write_variable_region,
72+
*IRB.GetInsertPoint()->getModule());
73+
return IRB.CreateCall(Decl, Args);
74+
}

IGC/VectorCompiler/lib/Utils/General/Types.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ IGCLLVM::FixedVectorType &vc::getVectorType(Type &Ty) {
130130
return cast<IGCLLVM::FixedVectorType>(Ty);
131131
return *IGCLLVM::FixedVectorType::get(&Ty, 1);
132132
}
133+
134+
Type *vc::setScalarType(Type &OrigTy, Type &ScalarTy) {
135+
IGC_ASSERT_MESSAGE(OrigTy.isFPOrFPVectorTy() || OrigTy.isIntOrIntVectorTy() ||
136+
OrigTy.isPtrOrPtrVectorTy(),
137+
"wrong argument: OrigType must be an int, float, pointer "
138+
"or vector of those");
139+
IGC_ASSERT_MESSAGE(
140+
ScalarTy.isFloatingPointTy() || ScalarTy.isIntegerTy() ||
141+
ScalarTy.isPointerTy(),
142+
"wrong argument: ScalarTy must be an int, float or pointer");
143+
if (!isa<IGCLLVM::FixedVectorType>(OrigTy))
144+
return &ScalarTy;
145+
return IGCLLVM::FixedVectorType::get(
146+
&ScalarTy, cast<IGCLLVM::FixedVectorType>(OrigTy).getNumElements());
147+
}

0 commit comments

Comments
 (0)