Skip to content

Commit 2e19957

Browse files
mateuszchudykigcbot
authored andcommitted
Add support for bitcast and addrspacecast to PromoteBools pass.
Add support for bitcast and addrspacecast to PromoteBools pass.
1 parent 5210817 commit 2e19957

File tree

3 files changed

+135
-4
lines changed

3 files changed

+135
-4
lines changed

IGC/AdaptorOCL/preprocess_spvir/PromoteBools.cpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ void PromoteBools::cleanUp(Module& module)
258258
{
259259
renameAndClean(alloca, it.second);
260260
}
261+
else if (auto bitcast = dyn_cast<BitCastInst>(it.first))
262+
{
263+
renameAndClean(bitcast, it.second);
264+
}
265+
else if (auto addrSpaceCast = dyn_cast<AddrSpaceCastInst>(it.first))
266+
{
267+
renameAndClean(addrSpaceCast, it.second);
268+
}
261269
}
262270

263271
std::vector<Instruction*> deadTruncs;
@@ -376,6 +384,14 @@ Value* PromoteBools::getOrCreatePromotedValue(Value* value)
376384
{
377385
newValue = promoteAlloca(alloca);
378386
}
387+
else if (auto bitcast = dyn_cast<BitCastInst>(value))
388+
{
389+
newValue = promoteBitCast(bitcast);
390+
}
391+
else if (auto addrSpaceCast = dyn_cast<AddrSpaceCastInst>(value))
392+
{
393+
newValue = promoteAddrSpaceCast(addrSpaceCast);
394+
}
379395

380396
if (newValue != value)
381397
{
@@ -481,22 +497,74 @@ GlobalVariable* PromoteBools::promoteGlobalVariable(GlobalVariable* globalVariab
481497
globalVariable->getType()->getPointerAddressSpace());
482498
}
483499

484-
AllocaInst* PromoteBools::promoteAlloca(AllocaInst* alloca)
500+
Value* PromoteBools::promoteAlloca(AllocaInst* alloca)
485501
{
486502
if (!alloca || !typeNeedsPromotion(alloca->getAllocatedType()))
487503
{
488504
return alloca;
489505
}
490506

507+
auto oldAllocaName = alloca->getName().str();
508+
alloca->setName(oldAllocaName + "_bitcast");
509+
491510
auto newAlloca = new AllocaInst(
492511
getOrCreatePromotedType(alloca->getAllocatedType()),
493512
alloca->getType()->getAddressSpace(),
494513
alloca->isArrayAllocation() ? alloca->getArraySize() : nullptr,
495-
alloca->getName(),
514+
oldAllocaName,
496515
alloca);
497516
newAlloca->setAlignment(IGCLLVM::getAlign(*alloca));
498517

499-
return newAlloca;
518+
IRBuilder<> builder(alloca);
519+
auto bitcast = builder.CreateBitCast(newAlloca, alloca->getType());
520+
alloca->replaceAllUsesWith(bitcast);
521+
522+
return bitcast;
523+
}
524+
525+
Value* PromoteBools::promoteBitCast(BitCastInst* bitcast)
526+
{
527+
if (!bitcast || !typeNeedsPromotion(bitcast->getDestTy()))
528+
{
529+
return bitcast;
530+
}
531+
532+
auto newType = getOrCreatePromotedType(bitcast->getDestTy());
533+
if (bitcast->getSrcTy() == newType)
534+
{
535+
auto result = bitcast->getOperand(0);
536+
537+
// swap names
538+
auto name = result->getName().str();
539+
result->setName(name + "_tmp");
540+
bitcast->setName(name);
541+
542+
return result;
543+
}
544+
545+
auto newBitCast = new BitCastInst(
546+
bitcast->getOperand(0),
547+
getOrCreatePromotedType(bitcast->getDestTy()),
548+
bitcast->getName(),
549+
bitcast
550+
);
551+
return newBitCast;
552+
}
553+
554+
Value* PromoteBools::promoteAddrSpaceCast(AddrSpaceCastInst* addrSpaceCast)
555+
{
556+
if (!addrSpaceCast || !typeNeedsPromotion(addrSpaceCast->getDestTy()))
557+
{
558+
return addrSpaceCast;
559+
}
560+
561+
auto newAddrSpaceCast = new AddrSpaceCastInst(
562+
addrSpaceCast->getOperand(0),
563+
getOrCreatePromotedType(addrSpaceCast->getDestTy()),
564+
addrSpaceCast->getName(),
565+
addrSpaceCast
566+
);
567+
return newAddrSpaceCast;
500568
}
501569

502570
Constant* PromoteBools::createPromotedConstant(Constant* constant)

IGC/AdaptorOCL/preprocess_spvir/PromoteBools.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ namespace IGC
5757
llvm::Value* getOrCreatePromotedValue(llvm::Value* value);
5858
llvm::Function* promoteFunction(llvm::Function* function);
5959
llvm::GlobalVariable* promoteGlobalVariable(llvm::GlobalVariable* globalVariable);
60-
llvm::AllocaInst* promoteAlloca(llvm::AllocaInst* alloca);
60+
llvm::Value* promoteAlloca(llvm::AllocaInst* alloca);
61+
llvm::Value* promoteBitCast(llvm::BitCastInst* bitcast);
62+
llvm::Value* promoteAddrSpaceCast(llvm::AddrSpaceCastInst* addrSpaceCast);
6163

6264
// Promoting contants
6365
llvm::Constant* createPromotedConstant(llvm::Constant* constant);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2022 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; RUN: igc_opt -igc-promote-bools -S %s -o %t.ll
10+
; RUN: FileCheck %s --input-file=%t.ll
11+
12+
define spir_func void @bitcast_i32_to_i1() {
13+
%allocated = alloca i32, align 1
14+
%casted = bitcast i32* %allocated to i1*
15+
%value = load i1, i1* %casted
16+
ret void
17+
}
18+
19+
; CHECK-LABEL: define spir_func void @bitcast_i32_to_i1()
20+
; CHECK-NEXT: %allocated = alloca i32, align 1
21+
; CHECK-NEXT: %casted = bitcast i32* %allocated to i8*
22+
; CHECK-NEXT: %value = load i8, i8* %casted
23+
24+
25+
define spir_func void @bitcast_i8_to_i1() {
26+
%allocated = alloca i8, align 1
27+
%casted = bitcast i8* %allocated to i1*
28+
%value = load i1, i1* %casted
29+
ret void
30+
}
31+
32+
; CHECK-LABEL: define spir_func void @bitcast_i8_to_i1()
33+
; CHECK-NEXT: %allocated = alloca i8, align 1
34+
; CHECK-NEXT: %value = load i8, i8* %allocated
35+
36+
37+
define spir_func void @addrspacecast_i32_to_i1() {
38+
%allocated = alloca i32, align 1
39+
%casted = addrspacecast i32* %allocated to i1 addrspace(4)*
40+
%value = load i1, i1 addrspace(4)* %casted
41+
ret void
42+
}
43+
44+
; CHECK-LABEL: define spir_func void @addrspacecast_i32_to_i1()
45+
; CHECK-NEXT: %allocated = alloca i32, align 1
46+
; CHECK-NEXT: %casted = addrspacecast i32* %allocated to i8 addrspace(4)*
47+
; CHECK-NEXT: %value = load i8, i8 addrspace(4)* %casted
48+
49+
50+
define spir_func void @addrspacecast_i1() {
51+
%allocated = alloca i1, align 1
52+
%casted = addrspacecast i1* %allocated to i1 addrspace(4)*
53+
%value = load i1, i1 addrspace(4)* %casted
54+
ret void
55+
}
56+
57+
; CHECK-LABEL: define spir_func void @addrspacecast_i1()
58+
; CHECK-NEXT: %allocated = alloca i8, align 1
59+
; CHECK-NEXT: %allocated_bitcast = bitcast i8* %allocated to i1*
60+
; CHECK-NEXT: %casted = addrspacecast i1* %allocated_bitcast to i8 addrspace(4)*
61+
; CHECK-NEXT: %value = load i8, i8 addrspace(4)* %casted

0 commit comments

Comments
 (0)