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
14 changes: 10 additions & 4 deletions lib/BitcastUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,16 @@ bool RemoveCstExprFromFunction(Function *F) {
I->setOperand(OperandId, VecNew);
} else if (auto CstExpr =
dyn_cast<ConstantExpr>(I->getOperand(OperandId))) {
auto Operand = CstExpr->getAsInstruction();
Operand->insertBefore(InsertBefore);
CheckInstruction(Operand);
I->setOperand(OperandId, Operand);
if (CstExpr->getOpcode() == Instruction::AddrSpaceCast &&
isa<ConstantPointerNull>(CstExpr->getOperand(0))) {
I->setOperand(OperandId, ConstantPointerNull::get(
cast<PointerType>(CstExpr->getType())));
Comment on lines +898 to +899

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using Constant::getNullValue is more idiomatic and cleaner than explicitly casting the type to PointerType and calling ConstantPointerNull::get. It simplifies the code and avoids the explicit cast.

        I->setOperand(OperandId, Constant::getNullValue(CstExpr->getType()));

} else {
auto Operand = CstExpr->getAsInstruction();
Operand->insertBefore(InsertBefore);
CheckInstruction(Operand);
I->setOperand(OperandId, Operand);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/InlineEntryPointsPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>

#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ bool clspv::InlineEntryPointsPass::RequiresInlining(Module &M) {
if (HasGeneric(I.getType()))
return true;
for (auto *op : I.operand_values()) {
if (HasGeneric(op->getType()))
if (!isa<ConstantPointerNull>(op) && HasGeneric(op->getType()))
return true;
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/InlineEntryPoints/null_pointer_no_inline.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; RUN: clspv-opt %s -o %t.ll --passes=inline-entry-points-pass
; RUN: FileCheck %s < %t.ll

target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
target triple = "spirv32-unknown-vulkan"

; CHECK-LABEL: define spir_func i32 @bar(
; CHECK-NOT: addrspace(4)
; CHECK: icmp eq ptr %p, null
; CHECK-LABEL: define spir_kernel void @foo(
; CHECK: call spir_func i32 @bar(

define spir_func i32 @bar(ptr %p, i32 %x) {
entry:
%cmp1 = icmp eq ptr %p, addrspacecast (ptr addrspace(4) null to ptr)
%cmp2 = icmp eq ptr addrspace(4) null, null
%cond = and i1 %cmp1, %cmp2
%res = select i1 %cond, i32 0, i32 %x
ret i32 %res
}

define spir_kernel void @foo(ptr addrspace(1) %out, i32 %n) {
entry:
%a = alloca i32, align 4
store i32 %n, ptr %a, align 4
%call = call spir_func i32 @bar(ptr %a, i32 %n)
store i32 %call, ptr addrspace(1) %out, align 4
ret void
}
Loading