Skip to content

Commit

Permalink
Fix mutable constexpr locals
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Dec 1, 2023
1 parent 5b5dc8a commit 0eae45d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/monomorph_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ and prep_let p id uniq e pass toplvl =
let cnt = new_id constant_uniq_state in
Hashtbl.add constant_tbl uniq (cnt, e1, toplvl);
({ p with vars = Vars.add un (Const uniq) p.vars }, Some uniq)
| { global = true; _ } | { const = true; _ } ->
| { global = true; _ } | { const = true; _ } when toplvl ->
(* Globals are 'preallocated' at module level *)
set_alloca p func.alloc;
let uniq = Module.unique_name ~mname:p.mname id uniq in
Expand Down
33 changes: 17 additions & 16 deletions test/functions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,6 @@ Don't copy mutable types in setup of tailrecursive functions
%r = type { i64 }

@schmu_rf = global %bref zeroinitializer, align 1
@schmu_i = internal global i64 0, align 8
@0 = private unnamed_addr constant { i64, i64, [6 x i8] } { i64 5, i64 5, [6 x i8] c"false\00" }
@1 = private unnamed_addr constant { i64, i64, [5 x i8] } { i64 4, i64 4, [5 x i8] c"true\00" }
@2 = private unnamed_addr constant { i64, i64, [4 x i8] } { i64 3, i64 3, [4 x i8] c"%s\0A\00" }
Expand Down Expand Up @@ -1763,22 +1762,24 @@ Don't copy mutable types in setup of tailrecursive functions
%12 = load i64*, i64** %8, align 8
%13 = load i64, i64* %12, align 8
call void (i8*, ...) @printf(i8* getelementptr (i8, i8* bitcast ({ i64, i64, [5 x i8] }* @3 to i8*), i64 16), i64 %13)
call void @schmu_change-int(i64* @schmu_i, i64 0)
%14 = load i64, i64* @schmu_i, align 8
call void (i8*, ...) @printf(i8* getelementptr (i8, i8* bitcast ({ i64, i64, [5 x i8] }* @3 to i8*), i64 16), i64 %14)
%15 = alloca i64*, align 8
%16 = call i8* @malloc(i64 24)
%17 = bitcast i8* %16 to i64*
store i64* %17, i64** %15, align 8
store i64 0, i64* %17, align 8
%cap4 = getelementptr i64, i64* %17, i64 1
%14 = alloca i64, align 8
store i64 0, i64* %14, align 8
call void @schmu_change-int(i64* %14, i64 0)
%15 = load i64, i64* %14, align 8
call void (i8*, ...) @printf(i8* getelementptr (i8, i8* bitcast ({ i64, i64, [5 x i8] }* @3 to i8*), i64 16), i64 %15)
%16 = alloca i64*, align 8
%17 = call i8* @malloc(i64 24)
%18 = bitcast i8* %17 to i64*
store i64* %18, i64** %16, align 8
store i64 0, i64* %18, align 8
%cap4 = getelementptr i64, i64* %18, i64 1
store i64 1, i64* %cap4, align 8
%18 = getelementptr i8, i8* %16, i64 16
call void @schmu_test(i64** %15, i64 0)
%19 = load i64*, i64** %15, align 8
%20 = load i64, i64* %19, align 8
call void (i8*, ...) @printf(i8* getelementptr (i8, i8* bitcast ({ i64, i64, [5 x i8] }* @3 to i8*), i64 16), i64 %20)
call void @__free_ai(i64** %15)
%19 = getelementptr i8, i8* %17, i64 16
call void @schmu_test(i64** %16, i64 0)
%20 = load i64*, i64** %16, align 8
%21 = load i64, i64* %20, align 8
call void (i8*, ...) @printf(i8* getelementptr (i8, i8* bitcast ({ i64, i64, [5 x i8] }* @3 to i8*), i64 16), i64 %21)
call void @__free_ai(i64** %16)
call void @__free_ai(i64** %8)
ret i64 0
}
Expand Down
6 changes: 6 additions & 0 deletions test/mutable_value_semantics.t/mutable_locals.smu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(defn dont-be-global (_)
(let ((localmut& false))
(print (fmt-str localmut))
(if (not localmut) (set &localmut !true))))

(iter-range 0 3 dont-be-global)
17 changes: 12 additions & 5 deletions test/mutable_value_semantics.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Test simple setting of mutable variables
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

@schmu_b = global i64 10, align 8
@schmu_b__2 = internal global i64 10, align 8
@schmu_a = global i64** null, align 8
@schmu_b__3 = global i64** null, align 8
@schmu_c = global i64* null, align 8
@0 = private unnamed_addr constant { i64, i64, [5 x i8] } { i64 4, i64 4, [5 x i8] c"%li\0A\00" }

define i64 @schmu_hmm() {
entry:
store i64 15, i64* @schmu_b__2, align 8
%0 = alloca i64, align 8
store i64 10, i64* %0, align 8
store i64 15, i64* %0, align 8
ret i64 15
}

Expand Down Expand Up @@ -1377,11 +1378,11 @@ Fix codegen
source_filename = "context"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

@schmu_x = internal global i64 10, align 8

define i64 @schmu_t() {
entry:
store i64 11, i64* @schmu_x, align 8
%0 = alloca i64, align 8
store i64 10, i64* %0, align 8
store i64 11, i64* %0, align 8
ret i64 11
}

Expand Down Expand Up @@ -1415,3 +1416,9 @@ Track unmutated binding warnings across projections
8 | (defn testfn (a& [b& int])
^^^^^^

Mutable locals must not be globals even if constexpr
$ schmu mutable_locals.smu
$ ./mutable_locals
false
false
false

0 comments on commit 0eae45d

Please sign in to comment.