@@ -1998,8 +1998,10 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
1998
1998
// in AST, but in IR one of them may be in opencl_private, and another in
1999
1999
// opencl_generic address space:
2000
2000
//
2001
- // int arr[5]; // automatic variable, default AS in AST, private AS in
2002
- // IR char* p = arr; // default AS in AST, generic AS in IR
2001
+ // int arr[5]; // automatic variable, default AS in AST,
2002
+ // // private AS in IR
2003
+ //
2004
+ // char* p = arr; // default AS in AST, generic AS in IR
2003
2005
//
2004
2006
if (E->getType ().getAddressSpace () != DestTy.getAddressSpace ())
2005
2007
llvm_unreachable (" wrong cast for pointers in different address spaces"
@@ -2821,6 +2823,53 @@ Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
2821
2823
// Binary Operators
2822
2824
// ===----------------------------------------------------------------------===//
2823
2825
2826
+ static Value *insertAddressSpaceCast (Value *V, unsigned NewAS) {
2827
+ auto *VTy = cast<llvm::PointerType>(V->getType ());
2828
+ if (VTy->getAddressSpace () == NewAS)
2829
+ return V;
2830
+
2831
+ llvm::PointerType *VTyNewAS =
2832
+ llvm::PointerType::get (VTy->getElementType (), NewAS);
2833
+
2834
+ if (auto *Constant = dyn_cast<llvm::Constant>(V))
2835
+ return llvm::ConstantExpr::getAddrSpaceCast (Constant, VTyNewAS);
2836
+
2837
+ llvm::Instruction *NewV =
2838
+ new llvm::AddrSpaceCastInst (V, VTyNewAS, V->getName () + " .ascast" );
2839
+ NewV->insertAfter (cast<llvm::Instruction>(V));
2840
+ return NewV;
2841
+ }
2842
+
2843
+ static void ensureSameAddrSpace (Value *&RHS, Value *&LHS,
2844
+ bool CanInsertAddrspaceCast,
2845
+ const LangOptions &Opts,
2846
+ const ASTContext &Context) {
2847
+ if (RHS->getType () == LHS->getType ())
2848
+ return ;
2849
+
2850
+ auto *RHSTy = dyn_cast<llvm::PointerType>(RHS->getType ());
2851
+ auto *LHSTy = dyn_cast<llvm::PointerType>(LHS->getType ());
2852
+ if (!RHSTy || !LHSTy || RHSTy->getAddressSpace () == LHSTy->getAddressSpace ())
2853
+ return ;
2854
+
2855
+ if (!CanInsertAddrspaceCast)
2856
+ // Pointers have different address spaces and we cannot do anything with
2857
+ // this.
2858
+ llvm_unreachable (" Pointers are expected to have the same address space." );
2859
+
2860
+ // Language rules define if it is legal to cast from one address space to
2861
+ // another, and which address space we should use as a "common
2862
+ // denominator". In SYCL, generic address space overlaps with all other
2863
+ // address spaces.
2864
+ if (Opts.SYCLIsDevice ) {
2865
+ unsigned GenericAS = Context.getTargetAddressSpace (LangAS::opencl_generic);
2866
+ RHS = insertAddressSpaceCast (RHS, GenericAS);
2867
+ LHS = insertAddressSpaceCast (LHS, GenericAS);
2868
+ } else
2869
+ llvm_unreachable (" Unable to find a common address space for "
2870
+ " two pointers." );
2871
+ }
2872
+
2824
2873
BinOpInfo ScalarExprEmitter::EmitBinOps (const BinaryOperator *E) {
2825
2874
TestAndClearIgnoreResultAssign ();
2826
2875
BinOpInfo Result;
@@ -3855,6 +3904,14 @@ Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,
3855
3904
RHS = Builder.CreateStripInvariantGroup (RHS);
3856
3905
}
3857
3906
3907
+ // Expression operands may have the same addrspace in AST, but different
3908
+ // addrspaces in LLVM IR, in which case an addrspacecast should be valid.
3909
+ bool CanInsertAddrspaceCast =
3910
+ LHSTy.getAddressSpace () == RHSTy.getAddressSpace ();
3911
+
3912
+ ensureSameAddrSpace (RHS, LHS, CanInsertAddrspaceCast, CGF.getLangOpts (),
3913
+ CGF.getContext ());
3914
+
3858
3915
Result = Builder.CreateICmp (UICmpOpc, LHS, RHS, " cmp" );
3859
3916
}
3860
3917
@@ -4160,53 +4217,6 @@ static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E,
4160
4217
// exist in the source-level program.
4161
4218
}
4162
4219
4163
- static Value *insertAddressSpaceCast (Value *V, unsigned NewAS) {
4164
- auto *VTy = cast<llvm::PointerType>(V->getType ());
4165
- if (VTy->getAddressSpace () == NewAS)
4166
- return V;
4167
-
4168
- llvm::PointerType *VTyNewAS =
4169
- llvm::PointerType::get (VTy->getElementType (), NewAS);
4170
-
4171
- if (auto *Constant = dyn_cast<llvm::Constant>(V))
4172
- return llvm::ConstantExpr::getAddrSpaceCast (Constant, VTyNewAS);
4173
-
4174
- llvm::Instruction *NewV =
4175
- new llvm::AddrSpaceCastInst (V, VTyNewAS, V->getName () + " .ascast" );
4176
- NewV->insertAfter (cast<llvm::Instruction>(V));
4177
- return NewV;
4178
- }
4179
-
4180
- static void ensureSameAddrSpace (Value *&RHS, Value *&LHS,
4181
- bool CanInsertAddrspaceCast,
4182
- const LangOptions &Opts,
4183
- const ASTContext &Context) {
4184
- if (RHS->getType () == LHS->getType ())
4185
- return ;
4186
-
4187
- auto *RHSTy = dyn_cast<llvm::PointerType>(RHS->getType ());
4188
- auto *LHSTy = dyn_cast<llvm::PointerType>(LHS->getType ());
4189
- if (!RHSTy || !LHSTy || RHSTy->getAddressSpace () == LHSTy->getAddressSpace ())
4190
- return ;
4191
-
4192
- if (!CanInsertAddrspaceCast)
4193
- // Pointers have different address spaces and we cannot do anything with
4194
- // this.
4195
- llvm_unreachable (" Pointers are expected to have the same address space." );
4196
-
4197
- // Language rules define if it is legal to cast from one address space to
4198
- // another, and which address space we should use as a "common
4199
- // denominator". In SYCL, generic address space overlaps with all other
4200
- // address spaces.
4201
- if (Opts.SYCLIsDevice ) {
4202
- unsigned GenericAS = Context.getTargetAddressSpace (LangAS::opencl_generic);
4203
- RHS = insertAddressSpaceCast (RHS, GenericAS);
4204
- LHS = insertAddressSpaceCast (LHS, GenericAS);
4205
- } else
4206
- llvm_unreachable (" Unable to find a common address space for "
4207
- " two pointers." );
4208
- }
4209
-
4210
4220
Value *ScalarExprEmitter::
4211
4221
VisitAbstractConditionalOperator (const AbstractConditionalOperator *E) {
4212
4222
TestAndClearIgnoreResultAssign ();
0 commit comments