Skip to content

[SYCL] Addrspace cast phi operands when lowering conditional operator #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
33 changes: 29 additions & 4 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4279,10 +4279,35 @@ EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
EmitBlock(contBlock);

if (lhs && rhs) {
llvm::PHINode *phi = Builder.CreatePHI(lhs->getPointer()->getType(),
2, "cond-lvalue");
phi->addIncoming(lhs->getPointer(), lhsBlock);
phi->addIncoming(rhs->getPointer(), rhsBlock);
llvm::Value *lhsPtr = lhs->getPointer();
llvm::Value *rhsPtr = rhs->getPointer();
if (rhsPtr->getType() != lhsPtr->getType()) {
if (!getLangOpts().SYCLIsDevice)
llvm_unreachable(
"Unable to find a common address space for two pointers.");

auto CastToAS = [](llvm::Value *V, llvm::BasicBlock *BB, unsigned AS) {
auto *Ty = cast<llvm::PointerType>(V->getType());
if (Ty->getAddressSpace() == AS)
return V;
llvm::IRBuilder<> Builder(BB->getTerminator());
auto *TyAS = llvm::PointerType::get(Ty->getElementType(), AS);
return Builder.CreatePointerBitCastOrAddrSpaceCast(V, TyAS);
};

// Language rules define if it is legal to cast from one address space
// to another, and which address space we should use as a "common
// denominator". In SYCL, generic address space overlaps with all other
// address spaces.
unsigned GenericAS =
getContext().getTargetAddressSpace(LangAS::opencl_generic);

lhsPtr = CastToAS(lhsPtr, lhsBlock, GenericAS);
rhsPtr = CastToAS(rhsPtr, rhsBlock, GenericAS);
}
llvm::PHINode *phi = Builder.CreatePHI(lhsPtr->getType(), 2, "cond-lvalue");
phi->addIncoming(lhsPtr, lhsBlock);
phi->addIncoming(rhsPtr, rhsBlock);
Address result(phi, std::min(lhs->getAlignment(), rhs->getAlignment()));
AlignmentSource alignSource =
std::max(lhs->getBaseInfo().getAlignmentSource(),
Expand Down
37 changes: 37 additions & 0 deletions clang/test/CodeGenSYCL/address-space-cond-op.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %clang_cc1 -x c++ -triple spir64-unknown-linux-sycldevice -std=c++11 -disable-llvm-passes -fsycl-is-device -emit-llvm %s -o - | FileCheck %s

// CHECK: [[STYPE:%.+]] = type { i16 }
struct S {
unsigned short x;
};

S foo(bool cond, S &lhs, S rhs) {
// CHECK-LABEL:@_Z3foobR1SS_
// CHECK: br i1 {{.+}}, label %[[BTRUE:.+]], label %[[BFALSE:.+]]
//
// CHECK: [[BTRUE]]:
// CHECK: %[[LHS:.+]] = load [[STYPE]] addrspace(4)*, [[STYPE]] addrspace(4)**
// CHECK: br label %[[BEND:.+]]
//
// CHECK: [[BFALSE]]:
// CHECK: %[[RHS:.+]] = addrspacecast [[STYPE]]* {{.+}} to [[STYPE]] addrspace(4)*
// CHECK: br label %[[BEND]]
//
// CHECK: [[BEND]]:
// CHECK: %{{.+}} = phi [[STYPE]] addrspace(4)* [ %[[LHS]], %[[BTRUE]] ], [ %[[RHS]], %[[BFALSE]] ]
S val = cond ? lhs : rhs;
return val;
}

template <typename name, typename Func>
__attribute__((sycl_kernel)) void kernel(Func kernelFunc) {
kernelFunc();
}

int main() {
kernel<class fake_kernel>([]() {
S lhs, rhs;
foo(true, lhs, rhs);
});
return 0;
}