authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-11-02 00:02:37-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-11-05 20:34:13-05:00
logce293c982e1b6d9db775e02fefb8905caa4ee748
treebb3a9a7ef4746efaeb656857aff91172520ee309
parent58789cb054b16e87d0a9b69ed8484cd30664fdb0

cbe: avoid collisions with builtins and intrinsics

Changes: - Add `isMangledIdent` to determine if `fmtIdent` would make any edits to the identifier - Any function that has a mangled identifier is referred to using the mangled identifer within the current file, but if it is exported the first export will be with the non-mangled name. - Add `zig_import` to import a symbol under a different name - Add a level of indirection to float function names. Now, they are referred to as `zig_float_fn_<float type>_<operation>`. The definitions in zig.h are wrapped with `zig_import` to import the symbol under the real name. The specific problem that sparked this change was the combination of `zig_libc_name_f80(name) __##name##x` with the input `fma`, resulting in `__fmax`, which is a new intrinsic in recent versions of cl.exe. With the above changes in place, compiler_rt can output the following: ``` static zig_weak_linkage_fn zig_f80 zig_e___fmax(zig_f80, zig_f80, zig_f80); zig_export(zig_weak_linkage_fn zig_f80 zig_e___fmax(zig_f80, zig_f80, zig_f80), __fmax, "__fmax"); ``` Within compiler_rt, `zig_e___fmax` is used to refer to the function, but consumers will import `__fmax`, which maps to their `zig_float_fn_f80_fma` definition from zig.h.

2 files changed, 89 insertions(+), 52 deletions(-)

lib/zig.h+42-28
......@@ -175,19 +175,33 @@ typedef char bool;
175175#endif
176176
177177#if zig_has_attribute(alias)
178#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol)))
178#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(#symbol)))
179179#elif _MSC_VER
180180#if _M_X64
181#define zig_export(sig, symbol, name) sig;\
182 __pragma(comment(linker, "/alternatename:" name "=" symbol ))
181#define zig_export(sig, symbol, name) zig_extern sig;\
182 __pragma(comment(linker, "/alternatename:" name "=" #symbol ))
183183#else /*_M_X64 */
184#define zig_export(sig, symbol, name) sig;\
185 __pragma(comment(linker, "/alternatename:_" name "=_" symbol ))
184#define zig_export(sig, symbol, name) zig_extern sig;\
185 __pragma(comment(linker, "/alternatename:" name "=" #symbol ))
186186#endif /*_M_X64 */
187187#else
188#define zig_export(sig, symbol, name) __asm(name " = " symbol)
188#define zig_export(sig, symbol, name) __asm(name " = " #symbol)
189189#endif
190190
191#if _MSC_VER
192#if _M_X64
193#define zig_import(sig, symbol, name) sig;\
194 __pragma(comment(linker, "/alternatename:" #symbol "=" #name ))
195#else /*_M_X64 */
196#define zig_import(sig, symbol, name) sig;\
197 __pragma(comment(linker, "/alternatename:_" #symbol "=_" #name ))
198#endif /*_M_X64 */
199#else
200#define zig_import(sig, symbol, name) zig_extern sig asm(#name);
201#endif
202
203#define zig_expand_import(sig, symbol, name) zig_import(sig, symbol, name)
204
191205#if zig_has_attribute(weak) || defined(zig_gnuc)
192206#define zig_weak_linkage __attribute__((weak))
193207#define zig_weak_linkage_fn __attribute__((weak))
......@@ -3330,31 +3344,31 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0)))
33303344 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, sub, -) \
33313345 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, mul, *) \
33323346 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, div, /) \
3333 zig_extern zig_f##w zig_libc_name_f##w(sqrt)(zig_f##w); \
3334 zig_extern zig_f##w zig_libc_name_f##w(sin)(zig_f##w); \
3335 zig_extern zig_f##w zig_libc_name_f##w(cos)(zig_f##w); \
3336 zig_extern zig_f##w zig_libc_name_f##w(tan)(zig_f##w); \
3337 zig_extern zig_f##w zig_libc_name_f##w(exp)(zig_f##w); \
3338 zig_extern zig_f##w zig_libc_name_f##w(exp2)(zig_f##w); \
3339 zig_extern zig_f##w zig_libc_name_f##w(log)(zig_f##w); \
3340 zig_extern zig_f##w zig_libc_name_f##w(log2)(zig_f##w); \
3341 zig_extern zig_f##w zig_libc_name_f##w(log10)(zig_f##w); \
3342 zig_extern zig_f##w zig_libc_name_f##w(fabs)(zig_f##w); \
3343 zig_extern zig_f##w zig_libc_name_f##w(floor)(zig_f##w); \
3344 zig_extern zig_f##w zig_libc_name_f##w(ceil)(zig_f##w); \
3345 zig_extern zig_f##w zig_libc_name_f##w(round)(zig_f##w); \
3346 zig_extern zig_f##w zig_libc_name_f##w(trunc)(zig_f##w); \
3347 zig_extern zig_f##w zig_libc_name_f##w(fmod)(zig_f##w, zig_f##w); \
3348 zig_extern zig_f##w zig_libc_name_f##w(fmin)(zig_f##w, zig_f##w); \
3349 zig_extern zig_f##w zig_libc_name_f##w(fmax)(zig_f##w, zig_f##w); \
3350 zig_extern zig_f##w zig_libc_name_f##w(fma)(zig_f##w, zig_f##w, zig_f##w); \
3347 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_sqrt(zig_f##w), zig_float_fn_f##w##_sqrt, zig_libc_name_f##w(sqrt)) \
3348 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_sin(zig_f##w), zig_float_fn_f##w##_sin, zig_libc_name_f##w(sin)) \
3349 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_cos(zig_f##w), zig_float_fn_f##w##_cos, zig_libc_name_f##w(cos)) \
3350 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_tan(zig_f##w), zig_float_fn_f##w##_tan, zig_libc_name_f##w(tan)) \
3351 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_exp(zig_f##w), zig_float_fn_f##w##_exp, zig_libc_name_f##w(exp)) \
3352 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_exp2(zig_f##w), zig_float_fn_f##w##_exp2, zig_libc_name_f##w(exp2)) \
3353 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log(zig_f##w), zig_float_fn_f##w##_log, zig_libc_name_f##w(log)) \
3354 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log2(zig_f##w), zig_float_fn_f##w##_log2, zig_libc_name_f##w(log2)) \
3355 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log10(zig_f##w), zig_float_fn_f##w##_log10, zig_libc_name_f##w(log10)) \
3356 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fabs(zig_f##w), zig_float_fn_f##w##_fabs, zig_libc_name_f##w(fabs)) \
3357 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_floor(zig_f##w), zig_float_fn_f##w##_floor, zig_libc_name_f##w(floor)) \
3358 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_ceil(zig_f##w), zig_float_fn_f##w##_ceil, zig_libc_name_f##w(ceil)) \
3359 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_round(zig_f##w), zig_float_fn_f##w##_round, zig_libc_name_f##w(round)) \
3360 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_trunc(zig_f##w), zig_float_fn_f##w##_trunc, zig_libc_name_f##w(trunc)) \
3361 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmod(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmod, zig_libc_name_f##w(fmod)) \
3362 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmin(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmin, zig_libc_name_f##w(fmin)) \
3363 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmax(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmax, zig_libc_name_f##w(fmax)) \
3364 zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fma(zig_f##w, zig_f##w, zig_f##w), zig_float_fn_f##w##_fma, zig_libc_name_f##w(fma)) \
33513365\
33523366 static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \
3353 return zig_libc_name_f##w(trunc)(zig_div_f##w(lhs, rhs)); \
3367 return zig_float_fn_f##w##_trunc(zig_div_f##w(lhs, rhs)); \
33543368 } \
33553369\
33563370 static inline zig_f##w zig_div_floor_f##w(zig_f##w lhs, zig_f##w rhs) { \
3357 return zig_libc_name_f##w(floor)(zig_div_f##w(lhs, rhs)); \
3371 return zig_float_fn_f##w##_floor(zig_div_f##w(lhs, rhs)); \
33583372 } \
33593373\
33603374 static inline zig_f##w zig_mod_f##w(zig_f##w lhs, zig_f##w rhs) { \
......@@ -3458,7 +3472,7 @@ zig_float_builtins(64)
34583472 zig_##Type zig_atomicrmw_desired; \
34593473 zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
34603474 do { \
3461 zig_atomicrmw_desired = zig_libc_name_##Type(fmin)(zig_atomicrmw_expected, arg); \
3475 zig_atomicrmw_desired = zig_float_fn_##Type##_fmin(zig_atomicrmw_expected, arg); \
34623476 } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
34633477 res = zig_atomicrmw_expected; \
34643478} while (0)
......@@ -3467,7 +3481,7 @@ zig_float_builtins(64)
34673481 zig_##Type zig_atomicrmw_desired; \
34683482 zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
34693483 do { \
3470 zig_atomicrmw_desired = zig_libc_name_##Type(fmax)(zig_atomicrmw_expected, arg); \
3484 zig_atomicrmw_desired = zig_float_fn_##Type##_fmax(zig_atomicrmw_expected, arg); \
34713485 } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
34723486 res = zig_atomicrmw_expected; \
34733487} while (0)
src/codegen/c.zig+47-24
......@@ -258,6 +258,20 @@ pub fn fmtIdent(ident: []const u8) std.fmt.Formatter(formatIdent) {
258258 return .{ .data = ident };
259259}
260260
261// Returns true if `formatIdent` would make any edits to ident.
262// This must be kept in sync with `formatIdent`.
263pub fn isMangledIdent(ident: []const u8, solo: bool) bool {
264 if (solo and isReservedIdent(ident)) return true;
265 for (ident, 0..) |c, i| {
266 switch (c) {
267 'a'...'z', 'A'...'Z', '_' => {},
268 '0'...'9' => if (i == 0) return true,
269 else => return true,
270 }
271 }
272 return false;
273}
274
261275/// This data is available when outputting .c code for a `InternPool.Index`
262276/// that corresponds to `func`.
263277/// It is not available when generating .h file.
......@@ -526,6 +540,7 @@ pub const DeclGen = struct {
526540 is_naked_fn: bool,
527541 /// This is a borrowed reference from `link.C`.
528542 fwd_decl: std.ArrayList(u8),
543
529544 error_msg: ?*Module.ErrorMsg,
530545 ctypes: CType.Store,
531546 /// Keeps track of anonymous decls that need to be rendered before this
......@@ -1598,7 +1613,7 @@ pub const DeclGen = struct {
15981613
15991614 switch (name) {
16001615 .export_index => |export_index| try dg.renderDeclName(w, fn_decl_index, export_index),
1601 .string => |string| try w.writeAll(string),
1616 .string => |string| try w.print("{ }", .{fmtIdent(string)}),
16021617 }
16031618
16041619 try renderTypeSuffix(
......@@ -1813,9 +1828,17 @@ pub const DeclGen = struct {
18131828 fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool {
18141829 const mod = dg.module;
18151830 return switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {
1816 .variable => |variable| mod.decl_exports.contains(variable.decl),
1831 .variable => |variable| {
1832 if (mod.decl_exports.get(variable.decl)) |exports| {
1833 return !isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true);
1834 } else return false;
1835 },
18171836 .extern_func => true,
1818 .func => |func| mod.decl_exports.contains(func.owner_decl),
1837 .func => |func| {
1838 if (mod.decl_exports.get(func.owner_decl)) |exports| {
1839 return !isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true);
1840 } else return false;
1841 },
18191842 else => unreachable,
18201843 };
18211844 }
......@@ -1925,9 +1948,9 @@ pub const DeclGen = struct {
19251948 try mod.markDeclAlive(decl);
19261949
19271950 if (mod.decl_exports.get(decl_index)) |exports| {
1928 try writer.print("{}", .{exports.items[export_index].opts.name.fmt(&mod.intern_pool)});
1951 try writer.print("{ }", .{fmtIdent(mod.intern_pool.stringToSlice(exports.items[export_index].opts.name))});
19291952 } else if (decl.getExternDecl(mod).unwrap()) |extern_decl_index| {
1930 try writer.print("{}", .{mod.declPtr(extern_decl_index).name.fmt(&mod.intern_pool)});
1953 try writer.print("{ }", .{fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(extern_decl_index).name))});
19311954 } else {
19321955 // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case),
19331956 // expand to 3x the length of its input, but let's cut it off at a much shorter limit.
......@@ -2564,16 +2587,19 @@ fn genExports(o: *Object) !void {
25642587 const fwd = o.dg.fwd_decl.writer();
25652588
25662589 const exports = mod.decl_exports.get(decl_index) orelse return;
2567 if (exports.items.len < 2) return;
2590
2591 const is_mangled = isMangledIdent(ip.stringToSlice(exports.items[0].opts.name), true);
2592 if (exports.items.len < 2 and !is_mangled) return;
25682593
25692594 switch (ip.indexToKey(tv.val.toIntern())) {
25702595 .func => {
2571 for (exports.items[1..], 1..) |@"export", i| {
2596 const start_i = 1 - @intFromBool(is_mangled);
2597 for (exports.items[start_i..], start_i..) |@"export", i| {
25722598 try fwd.writeAll("zig_export(");
25732599 if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn ");
25742600 try o.dg.renderFunctionSignature(fwd, decl_index, .forward, .{ .export_index = @as(u32, @intCast(i)) });
2575 try fwd.print(", {s}, {s});\n", .{
2576 fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),
2601 try fwd.print(", { }, {s});\n", .{
2602 fmtIdent(ip.stringToSlice(exports.items[0].opts.name)),
25772603 fmtStringLiteral(ip.stringToSlice(@"export".opts.name), null),
25782604 });
25792605 }
......@@ -2583,7 +2609,8 @@ fn genExports(o: *Object) !void {
25832609 unreachable;
25842610 },
25852611 .variable => |variable| {
2586 for (exports.items[1..], 1..) |@"export", i| {
2612 const start_i = 1 - @intFromBool(is_mangled);
2613 for (exports.items[start_i..], start_i..) |@"export", i| {
25872614 try fwd.writeAll("zig_export(");
25882615 if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage ");
25892616 const alias = ip.stringToSlice(@"export".opts.name);
......@@ -2595,8 +2622,8 @@ fn genExports(o: *Object) !void {
25952622 decl.alignment,
25962623 .complete,
25972624 );
2598 try fwd.print(", {s}, {s});\n", .{
2599 fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),
2625 try fwd.print(", { }, {s});\n", .{
2626 fmtIdent(ip.stringToSlice(exports.items[0].opts.name)),
26002627 fmtStringLiteral(alias, null),
26012628 });
26022629 }
......@@ -6861,9 +6888,9 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
68616888 try f.writeCValue(writer, accum, .Other);
68626889 switch (op) {
68636890 .float_op => |func| {
6864 try writer.writeAll(" = zig_libc_name_");
6891 try writer.writeAll(" = zig_float_fn_");
68656892 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6866 try writer.print("({s})(", .{func.operation});
6893 try writer.print("_{s}(", .{func.operation});
68676894 try f.writeCValue(writer, accum, .FunctionArgument);
68686895 try writer.writeAll(", ");
68696896 try f.writeCValue(writer, operand, .Other);
......@@ -7195,11 +7222,9 @@ fn unFloatOp(f: *Function, inst: Air.Inst.Index, operand: CValue, ty: Type, oper
71957222 const v = try Vectorize.start(f, inst, writer, ty);
71967223 try f.writeCValue(writer, local, .Other);
71977224 try v.elem(f, writer);
7198 try writer.writeAll(" = zig_libc_name_");
7225 try writer.writeAll(" = zig_float_fn_");
71997226 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
7200 try writer.writeByte('(');
7201 try writer.writeAll(operation);
7202 try writer.writeAll(")(");
7227 try writer.print("_{s}(", .{operation});
72037228 try f.writeCValue(writer, operand, .FunctionArgument);
72047229 try v.elem(f, writer);
72057230 try writer.writeAll(");\n");
......@@ -7234,11 +7259,9 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa
72347259 const v = try Vectorize.start(f, inst, writer, inst_ty);
72357260 try f.writeCValue(writer, local, .Other);
72367261 try v.elem(f, writer);
7237 try writer.writeAll(" = zig_libc_name_");
7262 try writer.writeAll(" = zig_float_fn_");
72387263 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
7239 try writer.writeByte('(');
7240 try writer.writeAll(operation);
7241 try writer.writeAll(")(");
7264 try writer.print("_{s}(", .{operation});
72427265 try f.writeCValue(writer, lhs, .FunctionArgument);
72437266 try v.elem(f, writer);
72447267 try writer.writeAll(", ");
......@@ -7268,9 +7291,9 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {
72687291 const v = try Vectorize.start(f, inst, writer, inst_ty);
72697292 try f.writeCValue(writer, local, .Other);
72707293 try v.elem(f, writer);
7271 try writer.writeAll(" = zig_libc_name_");
7294 try writer.writeAll(" = zig_float_fn_");
72727295 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
7273 try writer.writeAll("(fma)(");
7296 try writer.writeAll("_fma(");
72747297 try f.writeCValue(writer, mulend1, .FunctionArgument);
72757298 try v.elem(f, writer);
72767299 try writer.writeAll(", ");