[llvm] 88d0f47 - [test] Add test for hoisting to custom allocation function using allocsize

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 25 14:39:48 PDT 2021


Author: Philip Reames
Date: 2021-03-25T14:31:51-07:00
New Revision: 88d0f47b4f1962676c118487d847f16cf32e96f7

URL: https://github.com/llvm/llvm-project/commit/88d0f47b4f1962676c118487d847f16cf32e96f7
DIFF: https://github.com/llvm/llvm-project/commit/88d0f47b4f1962676c118487d847f16cf32e96f7.diff

LOG: [test] Add test for hoisting to custom allocation function using allocsize

The first is currently demonstrating a miscompile.

Added: 
    

Modified: 
    llvm/test/Transforms/LICM/hoist-alloc.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/LICM/hoist-alloc.ll b/llvm/test/Transforms/LICM/hoist-alloc.ll
index 2ea64a519861..78196f57d469 100644
--- a/llvm/test/Transforms/LICM/hoist-alloc.ll
+++ b/llvm/test/Transforms/LICM/hoist-alloc.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -basic-aa -licm < %s | FileCheck %s
+; RUN: opt -S -basic-aa -licm -use-dereferenceable-at-point-semantics=0 < %s | FileCheck %s
+; RUN: opt -S -basic-aa -licm -use-dereferenceable-at-point-semantics=1 < %s | FileCheck %s
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
@@ -321,3 +322,86 @@ for.end:
   ret i8 %phi
 }
 
+declare noalias i8* @my_alloc(i64) allocsize(0)
+
+; FIXME: While the result shown here is correct for the test case as written,
+; it would require context sensitive reasoning about frees which we don't
+; currently have to reach this result.  So, this is effectively demonstrating
+; a miscompile which needs investigated further.
+define i8 @test_hoist_allocsize() {
+; CHECK-LABEL: @test_hoist_allocsize(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[A_RAW:%.*]] = call nonnull i8* @my_alloc(i64 32)
+; CHECK-NEXT:    call void @init(i8* [[A_RAW]])
+; CHECK-NEXT:    [[ADDR:%.*]] = getelementptr i8, i8* [[A_RAW]], i32 31
+; CHECK-NEXT:    [[RES:%.*]] = load i8, i8* [[ADDR]], align 1
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    call void @use(i8 [[RES]])
+; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[IV_NEXT]], 200
+; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_END:%.*]], label [[FOR_BODY]]
+; CHECK:       for.end:
+; CHECK-NEXT:    [[RES_LCSSA:%.*]] = phi i8 [ [[RES]], [[FOR_BODY]] ]
+; CHECK-NEXT:    call void @free(i8* [[A_RAW]])
+; CHECK-NEXT:    ret i8 [[RES_LCSSA]]
+;
+entry:
+  %a.raw = call nonnull i8* @my_alloc(i64 32)
+  call void @init(i8* %a.raw)
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
+  call void @unknown() ;; may throw
+  %addr = getelementptr i8, i8* %a.raw, i32 31
+  %res = load i8, i8* %addr
+  call void @use(i8 %res)
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond = icmp eq i64 %iv.next, 200
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:
+  call void @free(i8* %a.raw)
+  ret i8 %res
+}
+
+define i8 @test_hoist_allocsize_leak() {
+; CHECK-LABEL: @test_hoist_allocsize_leak(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[A_RAW:%.*]] = call nonnull i8* @my_alloc(i64 32)
+; CHECK-NEXT:    call void @init(i8* [[A_RAW]])
+; CHECK-NEXT:    [[ADDR:%.*]] = getelementptr i8, i8* [[A_RAW]], i32 31
+; CHECK-NEXT:    [[RES:%.*]] = load i8, i8* [[ADDR]], align 1
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    call void @unknown()
+; CHECK-NEXT:    call void @use(i8 [[RES]])
+; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[IV_NEXT]], 200
+; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_END:%.*]], label [[FOR_BODY]]
+; CHECK:       for.end:
+; CHECK-NEXT:    [[RES_LCSSA:%.*]] = phi i8 [ [[RES]], [[FOR_BODY]] ]
+; CHECK-NEXT:    ret i8 [[RES_LCSSA]]
+;
+entry:
+  %a.raw = call nonnull i8* @my_alloc(i64 32)
+  call void @init(i8* %a.raw)
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
+  call void @unknown() ;; may throw
+  %addr = getelementptr i8, i8* %a.raw, i32 31
+  %res = load i8, i8* %addr
+  call void @use(i8 %res)
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond = icmp eq i64 %iv.next, 200
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:
+  ret i8 %res
+}


        


More information about the llvm-commits mailing list