zig does not produce llvm IR/bitcode
zig-linux-x86_64-0.9.0-dev.720+871f6343f
does not produce llvm IR/bitcode:
zig build-obj -femit-llvm-ir -fLLVM -lc hello.c
zig cc -S -emit-llvm hello.c
produces hello.ll:
clang-12 -S -emit-llvm hello.c
Here are the four cases covered by this issue:
stage1 backend
:x: no error, no .ll file produced
$ zig build-obj -femit-llvm-ir hello.zig
$ file hello.ll
hello.ll: cannot open `hello.ll' (No such file or directory)
Solving this is low priority.
stage2 llvm backend
:heavy_check_mark: Fixed in a5fb28070f37c2cad92ac8805bcc704e872fc538.
zig build-obj -fno-stage1 -femit-llvm-ir hello.zig
main zig CLI on a C file
:x: produces this error:
andy@ark ~/tmp> zig build-obj -femit-llvm-ir -lc hello.c
error(compilation): unable to copy /home/andy/.cache/zig/o/c7c6d7a85ba28e9c603ae0bcbb639dd6/hello.ll: FileNotFound
Error message improved in 7b78b4fff02e5f59da3034ee739c0eae99a01de5.
Solving this bug will require an enhancement to the glue code that invokes clang, because this command is intended to produce both an object file as well as an LLVM IR file, which will involve invoking clang multiple times.
zig cc
:heavy_check_mark: Fixed in d11f42c2b24e80388e1ea648ee97fae612fd76a4.
zig cc -S -emit-llvm hello.c
Whilst not directly related to this - I'm currently working on implementing bitcode parsing support - needed for our drop-in replacement of llvm-ar. Just thought that I would leave a note here, even though it's more about consuming bitcode than generating it.
EDIT: after brief chat with Jakub, other things are currently higher priority so delaying this until further notice.
zig cc
✔️ Fixed in d11f42c.
zig cc -S -emit-llvm hello.c
version: 0.11.0-dev.4271+1aacfa718
$> cat hello.c
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
$> zig cc -S -emit-llvm hello.c
LLVM Emit Object... [1] 18520 segmentation fault (core dumped) zig cc -S -emit-llvm hello.c
$> ls
hello.c hello.ll
$> zig build-obj -femit-llvm-ir -lc hello.c
error(compilation): unable to copy /home/kassane/.cache/zig/o/442c824a71f2319573a6ee54c34fcaca/hello.ll: FileNotFound
LLVM-IR output
; ModuleID = 'hello.c'
source_filename = "hello.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@.str = private unnamed_addr constant [14 x i8] c"Hello World!\0A\00", align 1, !dbg !0
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define i32 @main() #0 !dbg !16 {
%1 = alloca i32, align 4
store i32 0, ptr %1, align 4
%2 = call i32 (ptr, ...) @printf(ptr noundef @.str), !dbg !21
ret i32 0, !dbg !22
}
declare i32 @printf(ptr noundef, ...) #1
attributes #0 = { noinline nounwind optnone sspstrong uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="4" "target-cpu"="znver3" "target-features"="+64bit,+adx,+aes,+allow-light-256-bit,+avx,+avx2,+bmi,+bmi2,+branchfusion,+clflushopt,+clwb,+clzero,+cmov,+crc32,+cx16,+cx8,+f16c,+fast-15bytenop,+fast-bextr,+fast-lzcnt,+fast-movbe,+fast-scalar-fsqrt,+fast-scalar-shift-masks,+fast-variable-perlane-shuffle,+fast-vector-fsqrt,+fma,+fsgsbase,+fsrm,+fxsr,+invpcid,+lzcnt,+macrofusion,+mmx,+movbe,+mwaitx,+nopl,+pclmul,+pku,+popcnt,+prfchw,+rdpid,+rdpru,+rdrnd,+rdseed,+sahf,+sbb-dep-breaking,+sha,+shstk,+slow-shld,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+sse4a,+ssse3,+vaes,+vpclmulqdq,+vzeroupper,+wbnoinvd,+x87,+xsave,+xsavec,+xsaveopt,+xsaves,-16bit-mode,-32bit-mode,-3dnow,-3dnowa,-amx-bf16,-amx-fp16,-amx-int8,-amx-tile,-avx512bf16,-avx512bitalg,-avx512bw,-avx512cd,-avx512dq,-avx512er,-avx512f,-avx512fp16,-avx512ifma,-avx512pf,-avx512vbmi,-avx512vbmi2,-avx512vl,-avx512vnni,-avx512vp2intersect,-avx512vpopcntdq,-avxifma,-avxneconvert,-avxvnni,-avxvnniint8,-cldemote,-cmpccxadd,-enqcmd,-ermsb,-false-deps-getmant,-false-deps-lzcnt-tzcnt,-false-deps-mulc,-false-deps-mullq,-false-deps-perm,-false-deps-popcnt,-false-deps-range,-fast-11bytenop,-fast-7bytenop,-fast-gather,-fast-hops,-fast-shld-rotate,-fast-variable-crosslane-shuffle,-fast-vector-shift-masks,-fma4,-gfni,-harden-sls-ijmp,-harden-sls-ret,-hreset,-idivl-to-divb,-idivq-to-divl,-kl,-lea-sp,-lea-uses-ag,-lvi-cfi,-lvi-load-hardening,-lwp,-movdir64b,-movdiri,-pad-short-functions,-pconfig,-prefer-128-bit,-prefer-256-bit,-prefer-mask-registers,-prefetchi,-prefetchwt1,-ptwrite,-raoint,-retpoline,-retpoline-external-thunk,-retpoline-indirect-branches,-retpoline-indirect-calls,-rtm,-serialize,-seses,-sgx,-slow-3ops-lea,-slow-incdec,-slow-lea,-slow-pmaddwd,-slow-pmulld,-slow-two-mem-ops,-slow-unaligned-mem-16,-slow-unaligned-mem-32,-soft-float,-sse-unaligned-mem,-tagged-globals,-tbm,-tsxldtrk,-uintr,-use-glm-div-sqrt-costs,-use-slm-arith-costs,-waitpkg,-widekl,-xop" "tune-cpu"="generic" }
attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="4" "target-cpu"="znver3" "target-features"="+64bit,+adx,+aes,+allow-light-256-bit,+avx,+avx2,+bmi,+bmi2,+branchfusion,+clflushopt,+clwb,+clzero,+cmov,+crc32,+cx16,+cx8,+f16c,+fast-15bytenop,+fast-bextr,+fast-lzcnt,+fast-movbe,+fast-scalar-fsqrt,+fast-scalar-shift-masks,+fast-variable-perlane-shuffle,+fast-vector-fsqrt,+fma,+fsgsbase,+fsrm,+fxsr,+invpcid,+lzcnt,+macrofusion,+mmx,+movbe,+mwaitx,+nopl,+pclmul,+pku,+popcnt,+prfchw,+rdpid,+rdpru,+rdrnd,+rdseed,+sahf,+sbb-dep-breaking,+sha,+shstk,+slow-shld,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+sse4a,+ssse3,+vaes,+vpclmulqdq,+vzeroupper,+wbnoinvd,+x87,+xsave,+xsavec,+xsaveopt,+xsaves,-16bit-mode,-32bit-mode,-3dnow,-3dnowa,-amx-bf16,-amx-fp16,-amx-int8,-amx-tile,-avx512bf16,-avx512bitalg,-avx512bw,-avx512cd,-avx512dq,-avx512er,-avx512f,-avx512fp16,-avx512ifma,-avx512pf,-avx512vbmi,-avx512vbmi2,-avx512vl,-avx512vnni,-avx512vp2intersect,-avx512vpopcntdq,-avxifma,-avxneconvert,-avxvnni,-avxvnniint8,-cldemote,-cmpccxadd,-enqcmd,-ermsb,-false-deps-getmant,-false-deps-lzcnt-tzcnt,-false-deps-mulc,-false-deps-mullq,-false-deps-perm,-false-deps-popcnt,-false-deps-range,-fast-11bytenop,-fast-7bytenop,-fast-gather,-fast-hops,-fast-shld-rotate,-fast-variable-crosslane-shuffle,-fast-vector-shift-masks,-fma4,-gfni,-harden-sls-ijmp,-harden-sls-ret,-hreset,-idivl-to-divb,-idivq-to-divl,-kl,-lea-sp,-lea-uses-ag,-lvi-cfi,-lvi-load-hardening,-lwp,-movdir64b,-movdiri,-pad-short-functions,-pconfig,-prefer-128-bit,-prefer-256-bit,-prefer-mask-registers,-prefetchi,-prefetchwt1,-ptwrite,-raoint,-retpoline,-retpoline-external-thunk,-retpoline-indirect-branches,-retpoline-indirect-calls,-rtm,-serialize,-seses,-sgx,-slow-3ops-lea,-slow-incdec,-slow-lea,-slow-pmaddwd,-slow-pmulld,-slow-two-mem-ops,-slow-unaligned-mem-16,-slow-unaligned-mem-32,-soft-float,-sse-unaligned-mem,-tagged-globals,-tbm,-tsxldtrk,-uintr,-use-glm-div-sqrt-costs,-use-slm-arith-costs,-waitpkg,-widekl,-xop" "tune-cpu"="generic" }
!llvm.dbg.cu = !{!7}
!llvm.module.flags = !{!9, !10, !11, !12, !13, !14}
!llvm.ident = !{!15}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(scope: null, file: !2, line: 4, type: !3, isLocal: true, isDefinition: true)
!2 = !DIFile(filename: "hello.c", directory: "/home/kassane/Documentos/llama2_test/llama2.c")
!3 = !DICompositeType(tag: DW_TAG_array_type, baseType: !4, size: 112, elements: !5)
!4 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!5 = !{!6}
!6 = !DISubrange(count: 14)
!7 = distinct !DICompileUnit(language: DW_LANG_C11, file: !2, producer: "clang version 16.0.6 (https://github.com/ziglang/zig-bootstrap 13ba898f2b7946b9ba9e4fab0f59181eb3306ea6)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !8, splitDebugInlining: false, nameTableKind: None)
!8 = !{!0}
!9 = !{i32 7, !"Dwarf Version", i32 4}
!10 = !{i32 2, !"Debug Info Version", i32 3}
!11 = !{i32 1, !"wchar_size", i32 4}
!12 = !{i32 8, !"PIC Level", i32 2}
!13 = !{i32 7, !"uwtable", i32 2}
!14 = !{i32 7, !"frame-pointer", i32 2}
!15 = !{!"clang version 16.0.6 (https://github.com/ziglang/zig-bootstrap 13ba898f2b7946b9ba9e4fab0f59181eb3306ea6)"}
!16 = distinct !DISubprogram(name: "main", scope: !2, file: !2, line: 3, type: !17, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !7, retainedNodes: !20)
!17 = !DISubroutineType(types: !18)
!18 = !{!19}
!19 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!20 = !{}
!21 = !DILocation(line: 4, column: 2, scope: !16)
!22 = !DILocation(line: 5, column: 2, scope: !16)
Hello,
I have the same issue (segfault / unable to copy) with a 0.11.0 build from sources downloaded at https://ziglang.org/download