Skip to content

Commit d99d27f

Browse files
igorban-inteligcbot
authored andcommitted
Add internal uus_add instruction
.
1 parent 5f467c7 commit d99d27f

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

IGC/VectorCompiler/include/vc/InternalIntrinsics/Intrinsic_definitions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@
226226
"memory_effects":
227227
{ "access": "ModRef" }, },
228228

229+
## ``llvm.vc.internal.add.uus.sat`` : add instruction with saturation
230+
## ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
231+
##
232+
## * arg0: first input, any scalar/vector integer type, even i64 : unsigned
233+
## * arg1: second input, same type as arg0 : signed
234+
##
235+
## * Return value: result of addition, same type as arg0 : unsigned, saturated
236+
##
237+
## This intrinsic is used to implement the addition of unsigned and signed
238+
## integers with saturation.
239+
##
240+
"add_uus_sat" : { "result": "anyint",
241+
"arguments": [ 0, 0 ],
242+
"attributes": "None",
243+
"memory_effects":
244+
{ "access": "ModRef" }, },
245+
229246
## ``llvm.vc.internal.rsqrtm`` : computes component-wise reciprocal square root
230247
## ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
231248
##

IGC/VectorCompiler/lib/GenXCodeGen/GenXLowering.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,14 +3495,30 @@ bool GenXLowering::lowerUAddWithSat(CallInst *CI) {
34953495
return true;
34963496
}
34973497

3498+
// common subroutine for sub+sat: builds uadd (uus) with sat
3499+
static CallInst *buildUUSAddWithSat(CallInst *CI, Value *Arg0, Value *Arg1,
3500+
Instruction *InsertPoint) {
3501+
IGC_ASSERT(CI);
3502+
const DebugLoc &DL = CI->getDebugLoc();
3503+
Module *M = CI->getModule();
3504+
IRBuilder<> Builder(InsertPoint);
3505+
Type *ArgTypes[] = {Arg0->getType(), Arg1->getType()};
3506+
Value *Args[] = {Arg0, Arg1};
3507+
auto *Fn =
3508+
vc::getAnyDeclaration(M, vc::InternalIntrinsic::add_uus_sat, ArgTypes);
3509+
auto *Res = Builder.CreateCall(Fn, Args, CI->getName());
3510+
Res->setDebugLoc(DL);
3511+
return Res;
3512+
}
3513+
34983514
// llvm.usub.sat i.e. sat(a - b) we can treat as sat(a + (-b))
34993515
// i.e. we are building -b and then uadd with saturation
35003516
bool GenXLowering::lowerUSubWithSat(CallInst *CI) {
35013517
IGC_ASSERT(CI);
35023518
Value *Arg0 = CI->getArgOperand(0);
35033519
Value *Arg1 = IRBuilder<>(CI).CreateNeg(CI->getArgOperand(1), CI->getName());
3504-
auto *UUAddInst = buildUAddWithSat(CI, Arg0, Arg1, CI, /*IsSignedSrc*/ true);
3505-
CI->replaceAllUsesWith(UUAddInst);
3520+
Value *Res = buildUUSAddWithSat(CI, Arg0, Arg1, CI);
3521+
CI->replaceAllUsesWith(Res);
35063522
ToErase.push_back(CI);
35073523
return true;
35083524
}

IGC/VectorCompiler/lib/GenXCodeGen/Utils/cisa_gen_intrinsics.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@
151151
"src0": [ "GENERAL", 1 ],
152152
"src1": [ "GENERAL", 2 ]
153153
},
154+
"vc::InternalIntrinsic::add_uus_sat": {
155+
"opc": "ISA_ADD",
156+
"exec_size": [ "EXECSIZE" ],
157+
"elementwise": [ "ELEMENTWISE" ],
158+
"pred": [ "IMPLICITPRED" ],
159+
"dst": [ "GENERAL", "UNSIGNED", "SATURATION_SATURATE", 0 ],
160+
"src0": [ "GENERAL", "UNSIGNED", "MODIFIER_ARITH", 1 ],
161+
"src1": [ "GENERAL", "SIGNED", "MODIFIER_ARITH", 2 ]
162+
},
154163
"vc::InternalIntrinsic::cast_from_bf16": {
155164
"opc": "ISA_MOV",
156165
"exec_size": [ "EXECSIZE" ],

IGC/VectorCompiler/test/Lowering/llvm-intrinsics.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ define i32 @test_usub_sat(i32 %a, i32 %b)
9494
%res = tail call i32 @llvm.usub.sat.i32(i32 %a, i32 %b)
9595
; COM: this one is in terms on uuadd.sat
9696
; CHECK: [[NOTVAL:%[^ ]+]] = sub i32 0, %b
97-
; CHECK: call i32 @llvm.genx.usadd.sat.i32.i32(i32 %a, i32 [[NOTVAL]])
97+
; CHECK: call i32 @llvm.vc.internal.add.uus.sat.i32.i32(i32 %a, i32 [[NOTVAL]])
9898
ret i32 %res
9999
}
100100

@@ -203,4 +203,4 @@ define internal spir_func void @cttz_ctlz_i16(i16 %arg) {
203203

204204
%2 = call i16 @llvm.ctlz.i16(i16 %arg, i1 false)
205205
ret void
206-
}
206+
}

0 commit comments

Comments
 (0)