[m-rev.] diff: [CTGC] fix direct reuse with branching paths

Peter Wang novalazy at gmail.com
Wed Jul 30 12:59:03 AEST 2008


Branches: main

Fix a bug with direct reuse, when a cell dies in multiple branches.  After
the branching construct, the dead cell could be reused for a reconstruction
which is compatible with deconstructions in some of the branches, but not
all.

e.g. if a disjunction yields a dead cell with size 2 in one branch and size 1
in another branch, the bug meant that we might reuse that cell for a
construction requiring a size 2 cell.

compiler/structure_reuse.direct.choose_reuse.m:
	Fix the bug.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/bad_direct_reuse.exp:
tests/hard_coded/bad_direct_reuse.m:
	Add test case.

diff --git a/compiler/structure_reuse.direct.choose_reuse.m b/compiler/structure_reuse.direct.choose_reuse.m
index 91efce2..7bd82b7 100644
--- a/compiler/structure_reuse.direct.choose_reuse.m
+++ b/compiler/structure_reuse.direct.choose_reuse.m
@@ -911,9 +911,12 @@ beta_value = 1.
 
 verify_match(Background, NewVar, NewCons, NewArgs, PP, !Match) :-
     DeconSpecs = !.Match ^ decon_specs,
-    list.filter_map(compute_reuse_type(Background, NewVar, NewCons, NewArgs),
-        DeconSpecs, ReuseTypes),
     (
+        % The construction must be compatible with *all* deconstruction specs.
+        % Otherwise we may try to reuse a cell which is only compatible through
+        % one code path but not another.
+        list.map(compute_reuse_type(Background, NewVar, NewCons, NewArgs),
+            DeconSpecs, ReuseTypes),
         ReuseType = glb_reuse_types(ReuseTypes) % Can Fail.
     ->
         ConSpec = con(PP, ReuseType),
diff --git a/tests/hard_coded/Mercury.options b/tests/hard_coded/Mercury.options
index c3c7f76..f08bc36 100644
--- a/tests/hard_coded/Mercury.options
+++ b/tests/hard_coded/Mercury.options
@@ -1,5 +1,6 @@
 MCFLAGS-allow_stubs	=	--allow-stubs --no-warn-stubs --infer-all
 MCFLAGS-any_call_hoist_bug = --loop-invariants
+MCFLAGS-bad_direct_reuse =	--ctgc
 MCFLAGS-bad_indirect_reuse =	--ctgc --no-common-struct
 MCFLAGS-bad_indirect_reuse2 =	--ctgc --no-common-struct
 MCFLAGS-bad_indirect_reuse2b =	--ctgc --no-common-struct
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 03f941b..0e0f138 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -352,6 +352,7 @@ endif
 # grades.
 ifeq "$(findstring debug,$(GRADE))" ""
 	CTGC_PROGS = \
+		bad_direct_reuse \
 		bad_indirect_reuse \
 		bad_indirect_reuse2 \
 		bad_indirect_reuse2b \
diff --git a/tests/hard_coded/bad_direct_reuse.exp b/tests/hard_coded/bad_direct_reuse.exp
new file mode 100644
index 0000000..aa546d0
--- /dev/null
+++ b/tests/hard_coded/bad_direct_reuse.exp
@@ -0,0 +1 @@
+qualified(unqualified("builder"), "builder")
diff --git a/tests/hard_coded/bad_direct_reuse.m b/tests/hard_coded/bad_direct_reuse.m
new file mode 100644
index 0000000..46abc62
--- /dev/null
+++ b/tests/hard_coded/bad_direct_reuse.m
@@ -0,0 +1,42 @@
+%-----------------------------------------------------------------------------%
+% Regression test.
+
+:- module bad_direct_reuse.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+%-----------------------------------------------------------------------------%
+
+:- type sym_name
+    --->    unqualified(string)
+    ;       qualified(sym_name, string).
+
+main(!IO) :-
+    SymName0 = mk,
+    (
+        SymName0 = qualified(_, Unqual)
+    ;
+        SymName0 = unqualified(Unqual)
+    ),
+    % Bug: this reused the space for SymName0 even when
+    % SymName0 = unqualified(_), which is not wide enough.
+    SymName = qualified(mk, Unqual),
+    io.write(SymName, !IO),
+    io.nl(!IO).
+
+:- func mk = (sym_name::uo).
+:- pragma no_inline(mk/0).
+
+mk = SymName :-
+    copy(unqualified("builder"), SymName).
+
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=8 sts=4 sw=4 et


--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list