From: Yeting Kuo Date: Wed, 26 Apr 2023 04:55:50 +0000 (+0800) Subject: [ASAN] Support memory checks on vp.gather/scatter. X-Git-Tag: upstream/17.0.6~7091 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c80fe251d869d5304448cfa3bb855c653285de7f;p=platform%2Fupstream%2Fllvm.git [ASAN] Support memory checks on vp.gather/scatter. The patch supports vp.gather/scatter by allowing addresses being pointer vectors. And then we just need to check each active pointer element of those pointer vectors. Reviewed By: reames Differential Revision: https://reviews.llvm.org/D149245 --- diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 59dbfb3..511ac37 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1378,6 +1378,21 @@ void AddressSanitizer::getInterestingMemoryOperands( Stride); break; } + case Intrinsic::vp_gather: + case Intrinsic::vp_scatter: { + auto *VPI = cast(CI); + unsigned IID = CI->getIntrinsicID(); + bool IsWrite = IID == Intrinsic::vp_scatter; + if (IsWrite ? !ClInstrumentWrites : !ClInstrumentReads) + return; + unsigned PtrOpNo = *VPI->getMemoryPointerParamPos(IID); + Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType(); + MaybeAlign Alignment = VPI->getPointerAlignment(); + Interesting.emplace_back(I, PtrOpNo, IsWrite, Ty, Alignment, + VPI->getMaskParam(), + VPI->getVectorLengthParam()); + break; + } default: for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ArgNo++) { if (!ClInstrumentByval || !CI->isByValArgument(ArgNo) || @@ -1521,7 +1536,12 @@ void AddressSanitizer::instrumentMaskedLoadOrStore( } Value *InstrumentedAddress; - if (Stride) { + if (isa(Addr->getType())) { + assert( + cast(Addr->getType())->getElementType()->isPointerTy() && + "Expected vector of pointer."); + InstrumentedAddress = IRB.CreateExtractElement(Addr, Index); + } else if (Stride) { Index = IRB.CreateMul(Index, Stride); Addr = IRB.CreateBitCast(Addr, Type::getInt8PtrTy(*C)); InstrumentedAddress = IRB.CreateGEP(Type::getInt8Ty(*C), Addr, {Index}); diff --git a/llvm/test/Instrumentation/AddressSanitizer/asan-vp-load-store.ll b/llvm/test/Instrumentation/AddressSanitizer/asan-vp-load-store.ll index 8911f45..ee752c8 100644 --- a/llvm/test/Instrumentation/AddressSanitizer/asan-vp-load-store.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/asan-vp-load-store.ll @@ -444,3 +444,81 @@ define @scalable.strided.load.nxv4f32.align(ptr align 4 %p, %res = tail call @llvm.experimental.vp.strided.load.nxv4f32.i32(ptr %p, i32 4, %mask, i32 %evl) ret %res } + +; Test vp gather and scatter. +declare @llvm.vp.gather.nxv4f32.v4p0(, , i32) +declare void @llvm.vp.scatter.nxv4f32.v4p0(, , , i32) + +define @scalable.gather.nxv4f32( %vp, %mask, i32 %evl) sanitize_address { +; CHECK-LABEL: @scalable.gather.nxv4f32( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[EVL:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP1]], label [[TMP2:%.*]], label [[TMP12:%.*]] +; CHECK: 2: +; CHECK-NEXT: [[TMP3:%.*]] = zext i32 [[EVL]] to i64 +; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64() +; CHECK-NEXT: [[TMP5:%.*]] = mul i64 [[TMP4]], 4 +; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.umin.i64(i64 [[TMP3]], i64 [[TMP5]]) +; CHECK-NEXT: br label [[DOTSPLIT:%.*]] +; CHECK: .split: +; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[TMP2]] ], [ [[IV_NEXT:%.*]], [[TMP11:%.*]] ] +; CHECK-NEXT: [[TMP7:%.*]] = extractelement [[MASK:%.*]], i64 [[IV]] +; CHECK-NEXT: br i1 [[TMP7]], label [[TMP8:%.*]], label [[TMP11]] +; CHECK: 8: +; CHECK-NEXT: [[TMP9:%.*]] = extractelement [[VP:%.*]], i64 [[IV]] +; CHECK-NEXT: [[TMP10:%.*]] = ptrtoint ptr [[TMP9]] to i64 +; CHECK-NEXT: call void @__asan_load4(i64 [[TMP10]]) +; CHECK-NEXT: br label [[TMP11]] +; CHECK: 11: +; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1 +; CHECK-NEXT: [[IV_CHECK:%.*]] = icmp eq i64 [[IV_NEXT]], [[TMP6]] +; CHECK-NEXT: br i1 [[IV_CHECK]], label [[DOTSPLIT_SPLIT:%.*]], label [[DOTSPLIT]] +; CHECK: .split.split: +; CHECK-NEXT: br label [[TMP12]] +; CHECK: 12: +; CHECK-NEXT: [[RES:%.*]] = tail call @llvm.vp.gather.nxv4f32.nxv4p0( align 4 [[VP]], [[MASK]], i32 [[EVL]]) +; CHECK-NEXT: ret [[RES]] +; +; DISABLED-LABEL: @scalable.gather.nxv4f32( +; DISABLED-NEXT: [[RES:%.*]] = tail call @llvm.vp.gather.nxv4f32.nxv4p0( align 4 [[VP:%.*]], [[MASK:%.*]], i32 [[EVL:%.*]]) +; DISABLED-NEXT: ret [[RES]] +; + %res = tail call @llvm.vp.gather.nxv4f32.v4p0( align 4 %vp, %mask, i32 %evl) + ret %res +} + +define void @scalable.scatter.nxv4f32( %arg, %vp, %mask, i32 %evl) sanitize_address { +; CHECK-LABEL: @scalable.scatter.nxv4f32( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[EVL:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP1]], label [[TMP2:%.*]], label [[TMP12:%.*]] +; CHECK: 2: +; CHECK-NEXT: [[TMP3:%.*]] = zext i32 [[EVL]] to i64 +; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64() +; CHECK-NEXT: [[TMP5:%.*]] = mul i64 [[TMP4]], 4 +; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.umin.i64(i64 [[TMP3]], i64 [[TMP5]]) +; CHECK-NEXT: br label [[DOTSPLIT:%.*]] +; CHECK: .split: +; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[TMP2]] ], [ [[IV_NEXT:%.*]], [[TMP11:%.*]] ] +; CHECK-NEXT: [[TMP7:%.*]] = extractelement [[MASK:%.*]], i64 [[IV]] +; CHECK-NEXT: br i1 [[TMP7]], label [[TMP8:%.*]], label [[TMP11]] +; CHECK: 8: +; CHECK-NEXT: [[TMP9:%.*]] = extractelement [[VP:%.*]], i64 [[IV]] +; CHECK-NEXT: [[TMP10:%.*]] = ptrtoint ptr [[TMP9]] to i64 +; CHECK-NEXT: call void @__asan_store4(i64 [[TMP10]]) +; CHECK-NEXT: br label [[TMP11]] +; CHECK: 11: +; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1 +; CHECK-NEXT: [[IV_CHECK:%.*]] = icmp eq i64 [[IV_NEXT]], [[TMP6]] +; CHECK-NEXT: br i1 [[IV_CHECK]], label [[DOTSPLIT_SPLIT:%.*]], label [[DOTSPLIT]] +; CHECK: .split.split: +; CHECK-NEXT: br label [[TMP12]] +; CHECK: 12: +; CHECK-NEXT: tail call void @llvm.vp.scatter.nxv4f32.nxv4p0( [[ARG:%.*]], align 4 [[VP]], [[MASK]], i32 [[EVL]]) +; CHECK-NEXT: ret void +; +; DISABLED-LABEL: @scalable.scatter.nxv4f32( +; DISABLED-NEXT: tail call void @llvm.vp.scatter.nxv4f32.nxv4p0( [[ARG:%.*]], align 4 [[VP:%.*]], [[MASK:%.*]], i32 [[EVL:%.*]]) +; DISABLED-NEXT: ret void +; + tail call void @llvm.vp.scatter.nxv4f32.v4p0( %arg, align 4 %vp, %mask, i32 %evl) + ret void +}