Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void SPIRVEmitIntrinsics::propagateElemType(
DenseMap<Function *, CallInst *> Ptrcasts;
SmallVector<User *> Users(Op->users());
for (auto *U : Users) {
if (!isa<Instruction>(U) || isa<BitCastInst>(U) || isSpvIntrinsic(U))
if (!isa<Instruction>(U) || isSpvIntrinsic(U))
continue;
if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
continue;
Expand Down Expand Up @@ -506,7 +506,7 @@ void SPIRVEmitIntrinsics::propagateElemTypeRec(
return;
SmallVector<User *> Users(Op->users());
for (auto *U : Users) {
if (!isa<Instruction>(U) || isa<BitCastInst>(U) || isSpvIntrinsic(U))
if (!isa<Instruction>(U) || isSpvIntrinsic(U))
continue;
if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
continue;
Expand Down Expand Up @@ -958,6 +958,14 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(
return;
Uncomplete = isTodoType(I);
Ops.push_back(std::make_pair(Ref->getPointerOperand(), 0));
} else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
if (!isPointerTy(I->getType()))
return;
KnownElemTy = GR->findDeducedElementType(I);
if (!KnownElemTy)
return;
Uncomplete = isTodoType(I);
Ops.push_back(std::make_pair(Ref->getOperand(0), 0));
} else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
if (GR->findDeducedElementType(Ref->getPointerOperand()))
return;
Expand Down Expand Up @@ -1030,7 +1038,6 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(
}
}
}
TypeValidated.insert(I);
// Non-recursive update of types in the function uncomplete returns.
// This may happen just once per a function, the latch is a pair of
// findDeducedElementType(F) / addDeducedElementType(F, ...).
Expand All @@ -1043,6 +1050,7 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(
} else if (UncompleteRets) {
UncompleteRets->insert(I);
}
TypeValidated.insert(I);
return;
}
Uncomplete = isTodoType(CurrF);
Expand Down Expand Up @@ -1369,10 +1377,6 @@ void SPIRVEmitIntrinsics::replacePointerOperandWithPtrCast(
Instruction *I, Value *Pointer, Type *ExpectedElementType,
unsigned OperandToReplace, IRBuilder<> &B) {
TypeValidated.insert(I);
// If Pointer is the result of nop BitCastInst (ptr -> ptr), use the source
// pointer instead. The BitCastInst should be later removed when visited.
while (BitCastInst *BC = dyn_cast<BitCastInst>(Pointer))
Pointer = BC->getOperand(0);

// Do not emit spv_ptrcast if Pointer's element type is ExpectedElementType
Type *PointerElemTy = deduceElementTypeHelper(Pointer, false);
Expand Down Expand Up @@ -1759,8 +1763,7 @@ bool SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I,
IRBuilder<> &B,
bool UnknownElemTypeI8) {
reportFatalOnTokenType(I);
if (!isPointerTy(I->getType()) || !requireAssignType(I) ||
isa<BitCastInst>(I))
if (!isPointerTy(I->getType()) || !requireAssignType(I))
return false;

setInsertPointAfterDef(B, I);
Expand Down Expand Up @@ -1861,8 +1864,9 @@ void SPIRVEmitIntrinsics::insertSpirvDecorations(Instruction *I,
void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
IRBuilder<> &B) {
auto *II = dyn_cast<IntrinsicInst>(I);
if (II && II->getIntrinsicID() == Intrinsic::spv_const_composite &&
TrackConstants) {
bool IsConstComposite =
II && II->getIntrinsicID() == Intrinsic::spv_const_composite;
if (IsConstComposite && TrackConstants) {
setInsertPointAfterDef(B, I);
auto t = AggrConsts.find(I);
assert(t != AggrConsts.end());
Expand All @@ -1886,12 +1890,27 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
: B.SetInsertPoint(I);
BPrepared = true;
}
Type *OpTy = Op->getType();
Value *OpTyVal = Op;
if (Op->getType()->isTargetExtTy())
OpTyVal = PoisonValue::get(Op->getType());
auto *NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
{Op->getType(), OpTyVal->getType()}, Op,
OpTyVal, {}, B);
if (OpTy->isTargetExtTy())
OpTyVal = PoisonValue::get(OpTy);
CallInst *NewOp =
buildIntrWithMD(Intrinsic::spv_track_constant,
{OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);
Type *OpElemTy = nullptr;
if (!IsConstComposite && isPointerTy(OpTy) &&
(OpElemTy = GR->findDeducedElementType(Op)) != nullptr &&
OpElemTy != IntegerType::getInt8Ty(I->getContext())) {
buildAssignPtr(B, IntegerType::getInt8Ty(I->getContext()), NewOp);
SmallVector<Type *, 2> Types = {OpTy, OpTy};
SmallVector<Value *, 2> Args = {
NewOp, buildMD(PoisonValue::get(OpElemTy)),
B.getInt32(getPointerAddressSpace(OpTy))};
CallInst *PtrCasted =
B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
buildAssignPtr(B, OpElemTy, PtrCasted);
NewOp = PtrCasted;
}
I->setOperand(OpNo, NewOp);
}
}
Expand Down Expand Up @@ -2022,8 +2041,16 @@ void SPIRVEmitIntrinsics::processParamTypes(Function *F, IRBuilder<> &B) {
if (!isUntypedPointerTy(Arg->getType()))
continue;
Type *ElemTy = GR->findDeducedElementType(Arg);
if (!ElemTy && (ElemTy = deduceFunParamElementType(F, OpIdx)) != nullptr)
buildAssignPtr(B, ElemTy, Arg);
if (!ElemTy && (ElemTy = deduceFunParamElementType(F, OpIdx)) != nullptr) {
if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Arg)) {
DenseSet<std::pair<Value *, Value *>> VisitedSubst;
updateAssignType(AssignCI, Arg, PoisonValue::get(ElemTy));
propagateElemType(Arg, IntegerType::getInt8Ty(F->getContext()),
VisitedSubst);
} else {
buildAssignPtr(B, ElemTy, Arg);
}
}
}
}

Expand Down
235 changes: 235 additions & 0 deletions llvm/test/CodeGen/SPIRV/pointers/tangled-ret.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
; The only pass criterion is that spirv-val considers output valid.

; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}

%subgr = type { i64, i64 }
%t_range = type { %t_arr }
%t_arr = type { [1 x i64] }
%t_arr2 = type { [4 x i32] }

define internal spir_func noundef i32 @geti32() {
entry:
ret i32 100
}

define internal spir_func noundef i64 @geti64() {
entry:
ret i64 200
}

define internal spir_func void @enable_if(ptr addrspace(4) noundef align 8 dereferenceable_or_null(8) %this, i64 noundef %dim0) {
entry:
%this.addr = alloca ptr addrspace(4), align 8
%dim0.addr = alloca i64, align 8
store ptr addrspace(4) %this, ptr %this.addr, align 8
store i64 %dim0, ptr %dim0.addr, align 8
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%0 = load i64, ptr %dim0.addr, align 8
call spir_func void @enable_if_2(ptr addrspace(4) noundef align 8 dereferenceable_or_null(8) %this1, i64 noundef %0)
ret void
}


define internal spir_func void @test(ptr addrspace(4) noundef align 8 dereferenceable_or_null(16) %this, ptr addrspace(4) noundef align 4 dereferenceable(16) %bits, ptr noundef byval(%t_range) align 8 %pos) {
entry:
%this.addr = alloca ptr addrspace(4), align 8
%bits.addr = alloca ptr addrspace(4), align 8
%cur_pos = alloca i64, align 8
%__range4 = alloca ptr addrspace(4), align 8
%__begin0 = alloca ptr addrspace(4), align 8
%__end0 = alloca ptr addrspace(4), align 8
%cleanup.dest.slot = alloca i32, align 4
%elem = alloca ptr addrspace(4), align 8
%agg.tmp = alloca %t_range, align 8
%agg.tmp.ascast = addrspacecast ptr %agg.tmp to ptr addrspace(4)
store ptr addrspace(4) %this, ptr %this.addr, align 8
store ptr addrspace(4) %bits, ptr %bits.addr, align 8
%pos.ascast = addrspacecast ptr %pos to ptr addrspace(4)
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%call = call spir_func noundef i64 @getp(ptr addrspace(4) noundef align 8 dereferenceable_or_null(8) %pos.ascast, i32 noundef 0)
store i64 %call, ptr %cur_pos, align 8
%0 = load ptr addrspace(4), ptr %bits.addr, align 8
store ptr addrspace(4) %0, ptr %__range4, align 8
%1 = load ptr addrspace(4), ptr %__range4, align 8
%call2 = call spir_func noundef ptr addrspace(4) @beginp(ptr addrspace(4) noundef align 4 dereferenceable_or_null(16) %1)
store ptr addrspace(4) %call2, ptr %__begin0, align 8
%2 = load ptr addrspace(4), ptr %__range4, align 8
%call3 = call spir_func noundef ptr addrspace(4) @endp(ptr addrspace(4) noundef align 4 dereferenceable_or_null(16) %2)
store ptr addrspace(4) %call3, ptr %__end0, align 8
br label %for.cond

for.cond: ; preds = %for.inc, %entry
%3 = load ptr addrspace(4), ptr %__begin0, align 8
%4 = load ptr addrspace(4), ptr %__end0, align 8
%cmp = icmp ne ptr addrspace(4) %3, %4
br i1 %cmp, label %for.body, label %for.cond.cleanup

for.cond.cleanup: ; preds = %for.cond
br label %for.end

for.body: ; preds = %for.cond
%5 = load ptr addrspace(4), ptr %__begin0, align 8
store ptr addrspace(4) %5, ptr %elem, align 8
%6 = load i64, ptr %cur_pos, align 8
%call4 = call spir_func noundef i32 @maskp(ptr addrspace(4) noundef align 8 dereferenceable_or_null(16) %this1)
%conv = zext i32 %call4 to i64
%cmp5 = icmp ult i64 %6, %conv
br i1 %cmp5, label %if.then, label %if.else

if.then: ; preds = %for.body
%7 = load ptr addrspace(4), ptr %elem, align 8
%8 = load i64, ptr %cur_pos, align 8
call spir_func void @enable_if(ptr addrspace(4) noundef align 8 dereferenceable_or_null(8) %agg.tmp.ascast, i64 noundef %8)
call spir_func void @extract_bits(ptr addrspace(4) noundef align 8 dereferenceable_or_null(16) %this1, ptr addrspace(4) noundef align 4 dereferenceable(4) %7, ptr noundef byval(%t_range) align 8 %agg.tmp)
%9 = load i64, ptr %cur_pos, align 8
%add = add i64 %9, 32
store i64 %add, ptr %cur_pos, align 8
br label %if.end

if.else: ; preds = %for.body
%10 = load ptr addrspace(4), ptr %elem, align 8
store i32 0, ptr addrspace(4) %10, align 4
br label %if.end

if.end: ; preds = %if.else, %if.then
br label %for.inc

for.inc: ; preds = %if.end
%11 = load ptr addrspace(4), ptr %__begin0, align 8
%incdec.ptr = getelementptr inbounds nuw i32, ptr addrspace(4) %11, i32 1
store ptr addrspace(4) %incdec.ptr, ptr %__begin0, align 8
br label %for.cond

for.end: ; preds = %for.cond.cleanup
ret void
}

define internal spir_func noundef i64 @getp(ptr addrspace(4) noundef align 8 dereferenceable_or_null(8) %this, i32 noundef %dimension) {
entry:
%this.addr.i = alloca ptr addrspace(4), align 8
%dimension.addr.i = alloca i32, align 4
%retval = alloca i64, align 8
%this.addr = alloca ptr addrspace(4), align 8
%dimension.addr = alloca i32, align 4
%retval.ascast = addrspacecast ptr %retval to ptr addrspace(4)
store ptr addrspace(4) %this, ptr %this.addr, align 8
store i32 %dimension, ptr %dimension.addr, align 4
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%0 = load i32, ptr %dimension.addr, align 4
store ptr addrspace(4) %this1, ptr %this.addr.i, align 8
store i32 %0, ptr %dimension.addr.i, align 4
%this1.i = load ptr addrspace(4), ptr %this.addr.i, align 8
%common_array1 = bitcast ptr addrspace(4) %this1 to ptr addrspace(4)
%1 = load i32, ptr %dimension.addr, align 4
%idxprom = sext i32 %1 to i64
%arrayidx = getelementptr inbounds [1 x i64], ptr addrspace(4) %common_array1, i64 0, i64 %idxprom
%2 = load i64, ptr addrspace(4) %arrayidx, align 8
ret i64 %2
}

define internal spir_func noundef ptr addrspace(4) @beginp(ptr addrspace(4) noundef align 4 dereferenceable_or_null(16) %this) {
entry:
%retval = alloca ptr addrspace(4), align 8
%this.addr = alloca ptr addrspace(4), align 8
%retval.ascast = addrspacecast ptr %retval to ptr addrspace(4)
store ptr addrspace(4) %this, ptr %this.addr, align 8
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%MData1 = bitcast ptr addrspace(4) %this1 to ptr addrspace(4)
%arraydecay2 = bitcast ptr addrspace(4) %MData1 to ptr addrspace(4)
ret ptr addrspace(4) %arraydecay2
}

define internal spir_func noundef ptr addrspace(4) @endp(ptr addrspace(4) noundef align 4 dereferenceable_or_null(16) %this) {
entry:
%retval = alloca ptr addrspace(4), align 8
%this.addr = alloca ptr addrspace(4), align 8
%retval.ascast = addrspacecast ptr %retval to ptr addrspace(4)
store ptr addrspace(4) %this, ptr %this.addr, align 8
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%MData1 = bitcast ptr addrspace(4) %this1 to ptr addrspace(4)
%arraydecay2 = bitcast ptr addrspace(4) %MData1 to ptr addrspace(4)
%add.ptr = getelementptr inbounds nuw i32, ptr addrspace(4) %arraydecay2, i64 4
ret ptr addrspace(4) %add.ptr
}

define internal spir_func noundef i32 @maskp(ptr addrspace(4) noundef align 8 dereferenceable_or_null(16) %this) {
entry:
%retval = alloca i32, align 4
%this.addr = alloca ptr addrspace(4), align 8
%retval.ascast = addrspacecast ptr %retval to ptr addrspace(4)
store ptr addrspace(4) %this, ptr %this.addr, align 8
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%bits_num = getelementptr inbounds nuw %subgr, ptr addrspace(4) %this1, i32 0, i32 1
%0 = load i64, ptr addrspace(4) %bits_num, align 8
%conv = trunc i64 %0 to i32
ret i32 %conv
}

define internal spir_func void @enable_if_2(ptr addrspace(4) noundef align 8 dereferenceable_or_null(8) %this, i64 noundef %dim0) {
entry:
%this.addr = alloca ptr addrspace(4), align 8
%dim0.addr = alloca i64, align 8
store ptr addrspace(4) %this, ptr %this.addr, align 8
store i64 %dim0, ptr %dim0.addr, align 8
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%common_array1 = bitcast ptr addrspace(4) %this1 to ptr addrspace(4)
%0 = load i64, ptr %dim0.addr, align 8
store i64 %0, ptr addrspace(4) %common_array1, align 8
ret void
}

define internal spir_func void @extract_bits(ptr addrspace(4) noundef align 8 dereferenceable_or_null(16) %this, ptr addrspace(4) noundef align 4 dereferenceable(4) %bits, ptr noundef byval(%t_range) align 8 %pos) {
entry:
%this.addr = alloca ptr addrspace(4), align 8
%bits.addr = alloca ptr addrspace(4), align 8
%Res = alloca i64, align 8
store ptr addrspace(4) %this, ptr %this.addr, align 8
store ptr addrspace(4) %bits, ptr %bits.addr, align 8
%pos.ascast = addrspacecast ptr %pos to ptr addrspace(4)
%this1 = load ptr addrspace(4), ptr %this.addr, align 8
%Bits1 = bitcast ptr addrspace(4) %this1 to ptr addrspace(4)
%0 = load i64, ptr addrspace(4) %Bits1, align 8
store i64 %0, ptr %Res, align 8
%bits_num = getelementptr inbounds nuw %subgr, ptr addrspace(4) %this1, i32 0, i32 1
%1 = load i64, ptr addrspace(4) %bits_num, align 8
%call = call spir_func noundef i64 @geti64()
%2 = load i64, ptr %Res, align 8
%and = and i64 %2, %call
store i64 %and, ptr %Res, align 8
%call2 = call spir_func noundef i64 @geti64()
%call3 = call spir_func noundef i32 @geti32()
%conv = zext i32 %call3 to i64
%cmp = icmp ult i64 %call2, %conv
br i1 %cmp, label %if.then, label %if.else

if.else: ; preds = %entry
%3 = load ptr addrspace(4), ptr %bits.addr, align 8
store i32 0, ptr addrspace(4) %3, align 4
br label %if.end11

if.then: ; preds = %entry
%call4 = call spir_func noundef i64 @geti64()
%cmp5 = icmp ugt i64 %call4, 0
br i1 %cmp5, label %if.then6, label %if.end

if.then6: ; preds = %if.then
%call7 = call spir_func noundef i64 @geti64()
%4 = load i64, ptr %Res, align 8
%shr = lshr i64 %4, %call7
store i64 %shr, ptr %Res, align 8
br label %if.end

if.end: ; preds = %if.then6, %if.then
%call8 = call spir_func noundef i64 @geti64()
%5 = load i64, ptr %Res, align 8
%and9 = and i64 %5, %call8
store i64 %and9, ptr %Res, align 8
%6 = load i64, ptr %Res, align 8
%conv10 = trunc i64 %6 to i32
%7 = load ptr addrspace(4), ptr %bits.addr, align 8
store i32 %conv10, ptr addrspace(4) %7, align 4
br label %if.end11

if.end11: ; preds = %if.else, %if.end
ret void
}
Loading
Loading