Skip to content

Fix temporary variable naming collision when using translate-c with __builtin_convertvector. #24029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
@@ -1257,7 +1257,12 @@ fn transConvertVectorExpr(
const src_vector_ty = @as(*const clang.VectorType, @ptrCast(src_type));
const src_element_qt = src_vector_ty.getElementType();

const src_expr_node = try transExpr(c, &block_scope.base, src_expr, .used);
const src_expr_var_name = try block_scope.makeMangledName(c, "tmp");
try block_scope.statements.append(try Tag.var_simple.create(c.arena, .{
.name = src_expr_var_name,
.init = try transExpr(c, &block_scope.base, src_expr, .used),
}));
const src_expr_var_node = try Tag.identifier.create(c.arena, src_expr_var_name);

const dst_qt = expr.getTypeSourceInfo_getType();
const dst_type_node = try transQualType(c, &block_scope.base, dst_qt, base_stmt.getBeginLoc());
@@ -1273,7 +1278,7 @@ fn transConvertVectorExpr(
while (i < num_elements) : (i += 1) {
const mangled_name = try block_scope.makeMangledName(c, "tmp");
const value = try Tag.array_access.create(c.arena, .{
.lhs = src_expr_node,
.lhs = src_expr_var_node,
.rhs = try transCreateNodeNumber(c, i, .int),
});
const tmp_decl_node = try Tag.var_simple.create(c.arena, .{
@@ -1284,7 +1289,7 @@ fn transConvertVectorExpr(
}

const init_list = try c.arena.alloc(Node, num_elements);
for (init_list, 0..) |*init, init_index| {
for (init_list, block_scope.statements.items.len - num_elements..) |*init, init_index| {
const tmp_decl = block_scope.statements.items[init_index];
const name = tmp_decl.castTag(.var_simple).?.data.name;
init.* = try Tag.identifier.create(c.arena, name);
30 changes: 30 additions & 0 deletions test/cases/translate_c/builtin_convertvector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
typedef int i32x2 __attribute__((__vector_size__(8)));
typedef float f32x2 __attribute__((__vector_size__(8)));

f32x2 cast_function(i32x2 x) {
return (f32x2) __builtin_convertvector((i32x2) { x[0], x[1] }, f32x2);
}

// translate-c
// c_frontend=clang
//
// pub export fn cast_function(arg_x: i32x2) f32x2 {
// var x = arg_x;
// _ = &x;
// return blk: {
// const tmp = blk_1: {
// const tmp_2 = x[@as(c_uint, @intCast(@as(c_int, 0)))];
// const tmp_3 = x[@as(c_uint, @intCast(@as(c_int, 1)))];
// break :blk_1 i32x2{
// tmp_2,
// tmp_3,
// };
// };
// const tmp_1 = @as(f32, @floatFromInt(tmp[0]));
// const tmp_2 = @as(f32, @floatFromInt(tmp[1]));
// break :blk f32x2{
// tmp_1,
// tmp_2,
// };
// };
// }