zig icon indicating copy to clipboard operation
zig copied to clipboard

LLVM reports a broken module on tailcall

Open DanikVitek opened this issue 1 year ago • 4 comments

Zig Version

0.12.0-dev.3405+31791ae15

Steps to Reproduce and Observed Behavior

I'm updating my fork of the zitertools library to work with the latest nightly zig and after cleaning up some compile errors this is what I get:

$ zig build test
test
└─ run test
   └─ zig test Debug native failure
error: musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"cycle_iter.CycleIter(range_iter.RangeIter(u32)).next"(ptr sret({ i32, i8, [3 x i8] }) %2, ptr nonnull align 4 %21), !dbg !25675
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_iter.FilterIter(filter_iter.FilterIter(slice_iter.SliceIter(u32),(function 'even')),(function 'big')).next"(ptr sret({ i32, i8, [3 x i8] }) %2, ptr nonnull align 8 %27), !dbg !26009
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_iter.FilterContextIter(slice_iter.SliceIter(u32),u32,(function 'dividible')).next"(ptr sret({ i32, i8, [3 x i8] }) %2, ptr nonnull align 8 %29), !dbg !26096  
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_iter.FilterContextIter(slice_iter.SliceIter(u32),filter_iter.test.FilterContextIter closure.Closure,(function 'dividible')).next"(ptr sret({ i32, i8, [3 x i8] }) %2, ptr nonnull align 8 %28), !dbg !26187
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_iter.FilterIter(filter_iter.TestErrorIter,(function 'even')).next"(ptr sret({ { i64, i8, [7 x i8] }, i16, [6 x i8] }) %3, ptr %1, ptr nonnull align 8 %44), !dbg !26326
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_map_iter.FilterMapIter(slice_iter.SliceIter(u32),(function 'func')).next"(ptr sret({ i64, i8, [7 x i8] }) %2, ptr nonnull align 8 %30), !dbg !26805
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_map_iter.FilterMapContextIter(slice_iter.SliceIter(u32),u32,(function 'func')).next"(ptr sret({ i64, i8, [7 x i8] }) %2, ptr nonnull align 8 %32), !dbg !26909
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_map_iter.FilterMapContextIter(slice_iter.SliceIter(u32),filter_map_iter.test.FilterMapContextIter closure.Closure,(function 'func')).next"(ptr sret({ i64, i8, [7 x i8] }) %2, ptr nonnull align 8 %31), !dbg !27008
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_map_iter.FilterMapIter(filter_map_iter.TestErrorIter,(function 'func')).next"(ptr sret({ { i64, i8, [7 x i8] }, i16, [6 x i8] }) %3, ptr %1, ptr nonnull align 8 %47), !dbg !27140
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_map_iter.FilterMapIter(filter_map_iter.TestErrorIter,(function 'doubleEven')).next"(ptr sret({ { i64, i8, [7 x i8] }, i16, [6 x i8] }) %3, ptr %1, ptr nonnull align 8 %57), !dbg !27215
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"flatten_iter.FlattenIter(map_iter.MapIter(range_iter.RangeIter(u32),(function 'func'))).next"(ptr sret({ i32, i8, [3 x i8] }) %2, ptr nonnull align 4 %33), !dbg !27558
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @nth.nth__anon_9663(ptr sret({ i8, i8 }) %3, ptr nonnull align 1 %29, i64 %31), !dbg !30000
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @nth.nth__anon_9668(ptr sret({ { i64, i8, [7 x i8] }, i16, [6 x i8] }) %4, ptr %1, ptr nonnull align 8 %46, i64 %48), !dbg !30095
musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @"filter_iter.FilterIter(slice_iter.SliceIter(u32),(function 'even')).next"(ptr sret({ i32, i8, [3 x i8] }) %2, ptr nonnull align 8 %27), !dbg !36469
LLVM ERROR: Broken module found, compilation aborted!

error: the following command exited with error code 9:
C:\Users\x3665\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\zig.exe test -ODebug -Mroot=D:\MyStuff\Programming\Zig\zitertools\src\root.zig --cache-dir D:\MyStuff\Programming\Zig\zitertools\zig-cache --global-cache-dir C:\Users\x3665\AppData\Local\zig --name test --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
test transitive failure
└─ run test transitive failure
   └─ zig test Debug native failure
error: the following build command failed with exit code 1:
D:\MyStuff\Programming\Zig\zitertools\zig-cache\o\52bbdcbe24b616b49a5edbb82a5974db\build.exe C:\Users\x3665\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\zig.exe D:\MyStuff\Programming\Zig\zitertools D:\MyStuff\Programming\Zig\zitertools\zig-cache C:\Users\x3665\AppData\Local\zig --seed 0x1fb7161 -Zc92ceada8345803d test

Expected Behavior

Compilation should succeed and all of the tests should pass.

DanikVitek avatar Mar 22 '24 16:03 DanikVitek

I could try to obtain a minimal reproducible example

DanikVitek avatar Mar 22 '24 16:03 DanikVitek

HEAD is fe4e4697c69150688f3a537df4a257d7b56eb10d at time of writing

nektro avatar Mar 22 '24 19:03 nektro

commit of the corresponding nightly version is 31791ae15bfa8590cb235ce9d7c87a97d5136eef

DanikVitek avatar Mar 25 '24 17:03 DanikVitek

Can reproduce on 0.12.0-dev.3678+130fb5cb0. Relatively minimal reproducible example on GodBolt: Returning a struct from a tail-called function:

const Struct = struct { foo: usize, bar: usize };
var i: usize = 0;
pub fn next() Struct {
	i += 1;
	if (i > 3) return Struct {.foo = 1, .bar = 2};
	return @call(.always_tail, next, .{});
}
pub fn main() void {
    _ = next();
}

Error message:

LLVM Emit Object... musttail call must precede a ret with an optional bitcast
  musttail call fastcc void @tok2.next(ptr sret(%tok2.Struct) %1), !dbg !2294
LLVM ERROR: Broken module found, compilation aborted!

Expected behavior: Tail call works, or the Zig compiler (rather than LLVM) gives a descriptive error message reporting that this combination of features is not supported yet.

appgurueu avatar Apr 19 '24 14:04 appgurueu