<div dir="ltr">Forgot to mention. This same IR works fine on Linux and MacOS on the same CPU architecture.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 30, 2017 at 9:49 PM, Andrew Kelley <span dir="ltr"><<a href="mailto:superjoe30@gmail.com" target="_blank">superjoe30@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Below is attached a full IR module that can reproduce this issue, but the part to notice is this:</div><div><div><br></div><div>%Foo96Bits = type <{ i24, i24, i24, i24 }></div></div><div><br></div><div><div>define internal fastcc i16 @main.0.1() unnamed_addr #2 !dbg !113 {</div></div><div><div>Entry:</div><div>  %value = alloca %Foo96Bits, align 1</div><div>  %b = alloca i24, align 4</div><div>  %0 = bitcast %Foo96Bits* %value to i8*, !dbg !129</div><div>  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* bitcast (%Foo96Bits* @4 to i8*), i64 16, i32 1, i1 false), !dbg !129</div><div>  call void @llvm.dbg.declare(metadata %Foo96Bits* %value, metadata !118, metadata !75), !dbg !129</div><div>  %1 = getelementptr inbounds %Foo96Bits, %Foo96Bits* %value, i32 0, i32 0, !dbg !130</div><div>  %2 = load i24, i24* %1, align 1, !dbg !130</div><div>  store i24 %2, i24* %b, align 4, !dbg !131</div><div>  call void @llvm.dbg.declare(metadata i24* %b, metadata !127, metadata !75), !dbg !131</div><div>  ret i16 0, !dbg !132</div></div><div>}</div><div><br></div><div>Notice that the Foo96Bits struct is allocated with alignment value of 1. Later on there is a load which also has this alignment value of 1.</div><div><br></div><div>LLVM docs for alignment say: "An alignment of 1 is always safe."  (<a href="http://llvm.org/docs/LangRef.html#id189" target="_blank">http://llvm.org/docs/<wbr>LangRef.html#id189</a>)</div><div><br></div><div>This program when run crashes with a segfault.</div><div><br></div><div>However if I change the </div><div><br></div><div><div>  %value = alloca %Foo96Bits, align 1</div></div><div><br></div><div>to</div><div><div><br></div><div>  %value = alloca %Foo96Bits, align 32</div></div><div><br></div><div>and the corresponding load</div><div><br></div><div><div>  %2 = load i24, i24* %1, align 32</div></div><div><br></div><div>Then the program runs fine.</div><div><br></div><div><br></div><div>I was unable to reproduce the issue with this IR code and clang, because clang doesn't support cross compiling as seamlessly as my frontend:</div><div><div>$ clang -o test.exe test.ll -target x86_64-unknown-windows-msvc -fuse-ld=lld -Wl,-entry:_start</div></div><div><div>clang-5.0: warning: unable to find a Visual Studio installation; try running Clang from a developer command prompt [-Wmsvc-not-found]</div></div><div><div>/nix/store/<wbr>jxm0qg5cazlv8h7f7kp6cgk7ka7ag5<wbr>kr-lld-5.0.0rc3/bin/lld-link: error: could not open libcmt.lib: No such file or directory</div><div>clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)</div></div><div><br></div><div>But if you manage to get it linked I am sure this IR would trigger the issue. It even causes the segfault in wine64 in addition to native windows.</div><div><br></div><div><br></div><div><br></div><div>Can someone confirm this is a bug in llvm? Or is the documentation incorrect?</div><div><br></div><div><br></div><div>test.ll:</div><div><br></div><div><div><div>; ModuleID = 'test'</div><div>source_filename = "test"</div><div>target datalayout = "e-m:w-i64:64-f80:128-n8:16:<wbr>32:64-S128"</div><div>target triple = "x86_64-unknown-windows-msvc"</div><div><br></div><div>%"[]u8" = type { i8*, i64 }</div><div>%Foo96Bits = type <{ i24, i24, i24, i24 }></div><div><br></div><div>@link_libc = internal unnamed_addr constant i1 false, align 1</div><div>@want_start_symbol = internal unnamed_addr constant i1 true, align 1</div><div>@os = internal unnamed_addr constant i6 15, align 1</div><div>@is_windows = internal unnamed_addr constant i1 true, align 1</div><div>@user_main_fn = internal unnamed_addr global i16 ()* null, align 8</div><div>@0 = internal unnamed_addr constant i8* getelementptr inbounds ([24 x i8], [24 x i8]* @1, i64 0, i64 0), align 8</div><div>@1 = internal unnamed_addr constant [24 x i8] c"reached unreachable code", align 1</div><div>@2 = internal unnamed_addr constant %"[]u8" { i8* getelementptr inbounds ([24 x i8], [24 x i8]* @1, i64 0, i64 0), i64 24 }, align 8</div><div>@3 = internal unnamed_addr constant i16 ()** @user_main_fn, align 8</div><div>@4 = internal unnamed_addr constant %Foo96Bits zeroinitializer, align 1</div><div><br></div><div>; Function Attrs: nobuiltin noreturn nounwind</div><div>define internal fastcc void @panic(%"[]u8"* byval nonnull readonly) unnamed_addr #0 !dbg !60 {</div><div>Entry:</div><div>  call void @llvm.dbg.declare(metadata %"[]u8"* %0, metadata !74, metadata !75), !dbg !76</div><div>  br label %WhileCond, !dbg !77</div><div><br></div><div>WhileCond:                                        ; preds = %WhileCond, %Entry</div><div>  br label %WhileCond, !dbg !77</div><div>}</div><div><br></div><div>; Function Attrs: naked nobuiltin noreturn nounwind</div><div>define void @_start() #1 !dbg !80 {</div><div>Entry:</div><div>  call fastcc void @windowsCallMainAndExit(), !dbg !84</div><div>  unreachable, !dbg !84</div><div>}</div><div><br></div><div>; Function Attrs: nobuiltin nounwind</div><div>define internal i32 @main.0(i32, i8** nonnull, i8** nonnull) unnamed_addr #2 !dbg !87 {</div><div>Entry:</div><div>  %c_argc = alloca i32, align 4</div><div>  %c_argv = alloca i8**, align 8</div><div>  %c_envp = alloca i8**, align 8</div><div>  store i32 %0, i32* %c_argc, align 4</div><div>  call void @llvm.dbg.declare(metadata i32* %c_argc, metadata !94, metadata !75), !dbg !99</div><div>  store i8** %1, i8*** %c_argv, align 8</div><div>  call void @llvm.dbg.declare(metadata i8*** %c_argv, metadata !95, metadata !75), !dbg !100</div><div>  store i8** %2, i8*** %c_envp, align 8</div><div>  call void @llvm.dbg.declare(metadata i8*** %c_envp, metadata !97, metadata !75), !dbg !101</div><div>  call fastcc void @panic(%"[]u8"* @2), !dbg !102</div><div>  unreachable, !dbg !102</div><div>}</div><div><br></div><div>; Function Attrs: nobuiltin noreturn nounwind</div><div>define internal fastcc void @windowsCallMainAndExit() unnamed_addr #0 !dbg !106 {</div><div>Entry:</div><div>  %0 = alloca i16, align 2</div><div>  store i16 ()* @main.0.1, i16 ()** @user_main_fn, align 8, !dbg !107</div><div>  %1 = call fastcc i16 @main.0.1(), !dbg !109</div><div>  store i16 %1, i16* %0, align 2, !dbg !109</div><div>  %2 = load i16, i16* %0, align 2, !dbg !110</div><div>  %3 = icmp ne i16 %2, 0, !dbg !110</div><div>  br i1 %3, label %UnwrapErrError, label %UnwrapErrOk, !dbg !110</div><div><br></div><div>UnwrapErrError:                                   ; preds = %Entry</div><div>  call void @ExitProcess(i32 1), !dbg !111</div><div>  unreachable, !dbg !111</div><div><br></div><div>UnwrapErrOk:                                      ; preds = %Entry</div><div>  %4 = load i16, i16* %0, align 2, !dbg !110</div><div>  br label %UnwrapErrEnd, !dbg !110</div><div><br></div><div>UnwrapErrEnd:                                     ; preds = %UnwrapErrOk</div><div>  call void @ExitProcess(i32 0), !dbg !112</div><div>  unreachable, !dbg !112</div><div>}</div><div><br></div><div>; Function Attrs: nobuiltin nounwind</div><div>define internal fastcc i16 @main.0.1() unnamed_addr #2 !dbg !113 {</div><div>Entry:</div><div>  %value = alloca %Foo96Bits, align 1</div><div>  %b = alloca i24, align 4</div><div>  %0 = bitcast %Foo96Bits* %value to i8*, !dbg !129</div><div>  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* bitcast (%Foo96Bits* @4 to i8*), i64 16, i32 1, i1 false), !dbg !129</div><div>  call void @llvm.dbg.declare(metadata %Foo96Bits* %value, metadata !118, metadata !75), !dbg !129</div><div>  %1 = getelementptr inbounds %Foo96Bits, %Foo96Bits* %value, i32 0, i32 0, !dbg !130</div><div>  %2 = load i24, i24* %1, align 1, !dbg !130</div><div>  store i24 %2, i24* %b, align 4, !dbg !131</div><div>  call void @llvm.dbg.declare(metadata i24* %b, metadata !127, metadata !75), !dbg !131</div><div>  ret i16 0, !dbg !132</div><div>}</div><div><br></div><div>; Function Attrs: nobuiltin noreturn nounwind</div><div>declare void @ExitProcess(i32) #0</div><div><br></div><div>; Function Attrs: nounwind readnone speculatable</div><div>declare void @llvm.dbg.declare(metadata, metadata, metadata) #3</div><div><br></div><div>; Function Attrs: argmemonly nounwind</div><div>declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i32, i1) #4</div><div><br></div><div>attributes #0 = { nobuiltin noreturn nounwind "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-<wbr>leaf" }</div><div>attributes #1 = { naked nobuiltin noreturn nounwind "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-<wbr>leaf" }</div><div>attributes #2 = { nobuiltin nounwind "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-<wbr>leaf" }</div><div>attributes #3 = { nounwind readnone speculatable }</div><div>attributes #4 = { argmemonly nounwind }</div><div><br></div><div>!llvm.module.flags = !{!0}</div><div>!<a href="http://llvm.dbg.cu" target="_blank">llvm.dbg.cu</a> = !{!1}</div><div><br></div><div>!0 = !{i32 2, !"Debug Info Version", i32 3}</div><div>!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "zig 0.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !3, globals: !47)</div><div>!2 = !DIFile(filename: "test", directory: ".")</div><div>!3 = !{!4, !40}</div><div>!4 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "Os", scope: !5, file: !5, line: 1, baseType: !6, size: 8, align: 8, elements: !7)</div><div>!5 = !DIFile(filename: "builtin.zig", directory: "/home/andy/dev/zig/build/zig-<wbr>cache")</div><div>!6 = !DIBasicType(name: "u6", size: 8, encoding: DW_ATE_unsigned)</div><div>!7 = !{!8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31, !32, !33, !34, !35, !36, !37, !38, !39}</div><div>!8 = !DIEnumerator(name: "freestanding", value: 0)</div><div>!9 = !DIEnumerator(name: "ananas", value: 1)</div><div>!10 = !DIEnumerator(name: "cloudabi", value: 2)</div><div>!11 = !DIEnumerator(name: "darwin", value: 3)</div><div>!12 = !DIEnumerator(name: "dragonfly", value: 4)</div><div>!13 = !DIEnumerator(name: "freebsd", value: 5)</div><div>!14 = !DIEnumerator(name: "fuchsia", value: 6)</div><div>!15 = !DIEnumerator(name: "ios", value: 7)</div><div>!16 = !DIEnumerator(name: "kfreebsd", value: 8)</div><div>!17 = !DIEnumerator(name: "linux", value: 9)</div><div>!18 = !DIEnumerator(name: "lv2", value: 10)</div><div>!19 = !DIEnumerator(name: "macosx", value: 11)</div><div>!20 = !DIEnumerator(name: "netbsd", value: 12)</div><div>!21 = !DIEnumerator(name: "openbsd", value: 13)</div><div>!22 = !DIEnumerator(name: "solaris", value: 14)</div><div>!23 = !DIEnumerator(name: "windows", value: 15)</div><div>!24 = !DIEnumerator(name: "haiku", value: 16)</div><div>!25 = !DIEnumerator(name: "minix", value: 17)</div><div>!26 = !DIEnumerator(name: "rtems", value: 18)</div><div>!27 = !DIEnumerator(name: "nacl", value: 19)</div><div>!28 = !DIEnumerator(name: "cnk", value: 20)</div><div>!29 = !DIEnumerator(name: "bitrig", value: 21)</div><div>!30 = !DIEnumerator(name: "aix", value: 22)</div><div>!31 = !DIEnumerator(name: "cuda", value: 23)</div><div>!32 = !DIEnumerator(name: "nvcl", value: 24)</div><div>!33 = !DIEnumerator(name: "amdhsa", value: 25)</div><div>!34 = !DIEnumerator(name: "ps4", value: 26)</div><div>!35 = !DIEnumerator(name: "elfiamcu", value: 27)</div><div>!36 = !DIEnumerator(name: "tvos", value: 28)</div><div>!37 = !DIEnumerator(name: "watchos", value: 29)</div><div>!38 = !DIEnumerator(name: "mesa3d", value: 30)</div><div>!39 = !DIEnumerator(name: "contiki", value: 31)</div><div>!40 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "GlobalLinkage", scope: !5, file: !5, line: 138, baseType: !41, size: 8, align: 8, elements: !42)</div><div>!41 = !DIBasicType(name: "u3", size: 8, encoding: DW_ATE_unsigned)</div><div>!42 = !{!43, !44, !45, !46}</div><div>!43 = !DIEnumerator(name: "Internal", value: 0)</div><div>!44 = !DIEnumerator(name: "Strong", value: 1)</div><div>!45 = !DIEnumerator(name: "Weak", value: 2)</div><div>!46 = !DIEnumerator(name: "LinkOnce", value: 3)</div><div>!47 = !{!48, !51, !54, !56, !58}</div><div>!48 = !DIGlobalVariableExpression(<wbr>var: !49)</div><div>!49 = distinct !DIGlobalVariable(name: "link_libc", linkageName: "link_libc", scope: !5, file: !5, line: 200, type: !50, isLocal: true, isDefinition: true)</div><div>!50 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)</div><div>!51 = !DIGlobalVariableExpression(<wbr>var: !52)</div><div>!52 = distinct !DIGlobalVariable(name: "want_main_symbol", linkageName: "want_main_symbol", scope: !53, file: !53, line: 8, type: !50, isLocal: true, isDefinition: true)</div><div>!53 = !DIFile(filename: "bootstrap.zig", directory: "/home/andy/dev/zig/build/lib/<wbr>zig/std/special")</div><div>!54 = !DIGlobalVariableExpression(<wbr>var: !55)</div><div>!55 = distinct !DIGlobalVariable(name: "want_start_symbol", linkageName: "want_start_symbol", scope: !53, file: !53, line: 9, type: !50, isLocal: true, isDefinition: true)</div><div>!56 = !DIGlobalVariableExpression(<wbr>var: !57)</div><div>!57 = distinct !DIGlobalVariable(name: "os", linkageName: "os", scope: !5, file: !5, line: 195, type: !4, isLocal: true, isDefinition: true)</div><div>!58 = !DIGlobalVariableExpression(<wbr>var: !59)</div><div>!59 = distinct !DIGlobalVariable(name: "is_windows", linkageName: "is_windows", scope: !53, file: !53, line: 13, type: !50, isLocal: true, isDefinition: true)</div><div>!60 = distinct !DISubprogram(name: "panic", scope: !61, file: !61, line: 47, type: !62, isLocal: true, isDefinition: true, scopeLine: 47, isOptimized: false, unit: !1, variables: !73)</div><div>!61 = !DIFile(filename: "test.zig", directory: "/home/andy/dev/zig/build")</div><div>!62 = !DISubroutineType(types: !63)</div><div>!63 = !{!64, !65}</div><div>!64 = !DIBasicType(name: "void", encoding: DW_ATE_unsigned)</div><div>!65 = !DIDerivedType(tag: DW_TAG_pointer_type, name: "&[]const u8", baseType: !66, size: 64, align: 64)</div><div>!66 = !DICompositeType(tag: DW_TAG_structure_type, name: "[]u8", size: 128, align: 64, elements: !67)</div><div>!67 = !{!68, !71}</div><div>!68 = !DIDerivedType(tag: DW_TAG_member, name: "ptr", scope: !66, baseType: !69, size: 64, align: 64)</div><div>!69 = !DIDerivedType(tag: DW_TAG_pointer_type, name: "&u8", baseType: !70, size: 64, align: 64)</div><div>!70 = !DIBasicType(name: "u8", size: 8, encoding: DW_ATE_unsigned_char)</div><div>!71 = !DIDerivedType(tag: DW_TAG_member, name: "len", scope: !66, baseType: !72, size: 64, align: 64, offset: 64)</div><div>!72 = !DIBasicType(name: "usize", size: 64, encoding: DW_ATE_unsigned)</div><div>!73 = !{!74}</div><div>!74 = !DILocalVariable(name: "msg", arg: 1, scope: !60, file: !61, line: 47, type: !66)</div><div>!75 = !DIExpression()</div><div>!76 = !DILocation(line: 47, column: 14, scope: !60)</div><div>!77 = !DILocation(line: 47, column: 45, scope: !78)</div><div>!78 = distinct !DILexicalBlock(scope: !79, file: !61, line: 47, column: 43)</div><div>!79 = distinct !DILexicalBlock(scope: !60, file: !61, line: 47, column: 14)</div><div>!80 = distinct !DISubprogram(name: "_start", scope: !53, file: !53, line: 15, type: !81, isLocal: false, isDefinition: true, scopeLine: 15, isOptimized: false, unit: !1, variables: !83)</div><div>!81 = !DISubroutineType(types: !82)</div><div>!82 = !{!64}</div><div>!83 = !{}</div><div>!84 = !DILocation(line: 22, column: 31, scope: !85)</div><div>!85 = distinct !DILexicalBlock(scope: !86, file: !53, line: 21, column: 21)</div><div>!86 = distinct !DILexicalBlock(scope: !80, file: !53, line: 15, column: 40)</div><div>!87 = distinct !DISubprogram(name: "main", scope: !53, file: !53, line: 63, type: !88, isLocal: true, isDefinition: true, scopeLine: 63, isOptimized: false, unit: !1, variables: !93)</div><div>!88 = !DISubroutineType(types: !89)</div><div>!89 = !{!90, !90, !91, !92}</div><div>!90 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_signed)</div><div>!91 = !DIDerivedType(tag: DW_TAG_pointer_type, name: "&&u8", baseType: !69, size: 64, align: 64)</div><div>!92 = !DIDerivedType(tag: DW_TAG_pointer_type, name: "&?&u8", baseType: !69, size: 64, align: 64)</div><div>!93 = !{!94, !95, !97}</div><div>!94 = !DILocalVariable(name: "c_argc", arg: 1, scope: !87, file: !53, line: 63, type: !90)</div><div>!95 = !DILocalVariable(name: "c_argv", arg: 2, scope: !96, file: !53, line: 63, type: !91)</div><div>!96 = distinct !DILexicalBlock(scope: !87, file: !53, line: 63, column: 16)</div><div>!97 = !DILocalVariable(name: "c_envp", arg: 3, scope: !98, file: !53, line: 63, type: !92)</div><div>!98 = distinct !DILexicalBlock(scope: !96, file: !53, line: 63, column: 29)</div><div>!99 = !DILocation(line: 63, column: 16, scope: !87)</div><div>!100 = !DILocation(line: 63, column: 29, scope: !96)</div><div>!101 = !DILocation(line: 63, column: 43, scope: !98)</div><div>!102 = !DILocation(line: 66, column: 9, scope: !103)</div><div>!103 = distinct !DILexicalBlock(scope: !104, file: !53, line: 64, column: 28)</div><div>!104 = distinct !DILexicalBlock(scope: !105, file: !53, line: 63, column: 65)</div><div>!105 = distinct !DILexicalBlock(scope: !98, file: !53, line: 63, column: 43)</div><div>!106 = distinct !DISubprogram(name: "windowsCallMainAndExit", scope: !53, file: !53, line: 37, type: !81, isLocal: true, isDefinition: true, scopeLine: 37, isOptimized: false, unit: !1, variables: !83)</div><div>!107 = !DILocation(line: 38, column: 28, scope: !108)</div><div>!108 = distinct !DILexicalBlock(scope: !106, file: !53, line: 37, column: 41)</div><div>!109 = !DILocation(line: 39, column: 14, scope: !108)</div><div>!110 = !DILocation(line: 39, column: 17, scope: !108)</div><div>!111 = !DILocation(line: 39, column: 46, scope: !108)</div><div>!112 = !DILocation(line: 40, column: 31, scope: !108)</div><div>!113 = distinct !DISubprogram(name: "main", scope: !61, file: !61, line: 14, type: !114, isLocal: true, isDefinition: true, scopeLine: 14, isOptimized: false, unit: !1, variables: !117)</div><div>!114 = !DISubroutineType(types: !115)</div><div>!115 = !{!116}</div><div>!116 = !DIBasicType(name: "u16", size: 16, encoding: DW_ATE_unsigned)</div><div>!117 = !{!118, !127}</div><div>!118 = !DILocalVariable(name: "value", scope: !119, file: !61, line: 16, type: !120)</div><div>!119 = distinct !DILexicalBlock(scope: !113, file: !61, line: 14, column: 24)</div><div>!120 = !DICompositeType(tag: DW_TAG_structure_type, name: "Foo96Bits", scope: !61, file: !61, line: 7, size: 128, align: 128, elements: !121)</div><div>!121 = !{!122, !124, !125, !126}</div><div>!122 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !120, file: !61, line: 8, baseType: !123, size: 24, align: 1)</div><div>!123 = !DIBasicType(name: "u24", size: 24, encoding: DW_ATE_unsigned)</div><div>!124 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !120, file: !61, line: 9, baseType: !123, size: 24, align: 1, offset: 32)</div><div>!125 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !120, file: !61, line: 10, baseType: !123, size: 24, align: 1, offset: 64)</div><div>!126 = !DIDerivedType(tag: DW_TAG_member, name: "d", scope: !120, file: !61, line: 11, baseType: !123, size: 24, align: 1, offset: 96)</div><div>!127 = !DILocalVariable(name: "b", scope: !128, file: !61, line: 22, type: !123)</div><div>!128 = distinct !DILexicalBlock(scope: !119, file: !61, line: 16, column: 5)</div><div>!129 = !DILocation(line: 16, column: 5, scope: !119)</div><div>!130 = !DILocation(line: 22, column: 20, scope: !128)</div><div>!131 = !DILocation(line: 22, column: 5, scope: !128)</div><div>!132 = !DILocation(line: 14, column: 24, scope: !113)</div></div><div><br></div><div><br></div></div></div>
</blockquote></div><br></div>