Skip to content

Fix array support (-Oarray optimization)#143

Open
alastairreid wants to merge 4 commits into
masterfrom
areid/array-return
Open

Fix array support (-Oarray optimization)#143
alastairreid wants to merge 4 commits into
masterfrom
areid/array-return

Conversation

@alastairreid
Copy link
Copy Markdown

@alastairreid alastairreid commented Apr 27, 2026

This transformation fixes array support by wrapping arrays in structs.
This works because C does not support array assignment (because arrays are promoted to pointers) but it does support struct assignment.

Fixes #41

This change makes it easier to figure out the type of array literals
which is required for the array wrapping transformation xform_array.
This transformation wraps arrays in records.
The purpose of this is to simplify C code generation which was
incorrectly generating 'x = y;' to copy an array.
Comment thread libISA/xform_arrays.ml
* This makes code generation easier because it allows
* us to copy arrays using assignment.
*
* Copyright (C) 2025-2025 Intel Corporation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (C) 2025-2025 Intel Corporation
* Copyright (C) 2026-2026 Intel Corporation

Comment thread libISA/xform_arrays.mli
(****************************************************************
* ISA array wrapping transform
*
* Copyright (C) 2025-2025 Intel Corporation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (C) 2025-2025 Intel Corporation
* Copyright (C) 2026-2026 Intel Corporation

Comment thread bin/iic.py
if args.O0:
script = []
script.append(":filter_unlisted_functions imports")
script.append(":filter_reachable_from --no-keep-builtins exports")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

@@ -0,0 +1,37 @@
// RUN: %aslrun -Oarrays %s | filecheck %s
// Copyright (C) 2023-2025 Intel Corporation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Also in other tests.

// Test returning a record containing an array
function Test(a : array [4] of Bits(32), i : Integer) -> R
begin
// var r := R{ x => a };
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// var r := R{ x => a };

Comment thread libISA/xform_arrays.ml
ds' @ tuple_decls

(****************************************************************
* Command: :xform_tuples
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Command: :xform_tuples
* Command: :xform_arrays

Comment thread libISA/xform_arrays.ml
end

let xform_decls (ds : AST.declaration list) : AST.declaration list =
let replacer = new replaceArrayClass None in
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let replacer = new replaceArrayClass None in
let replacer = new replaceArrayClass in

tc not used.

Comment thread libISA/xform_arrays.ml
let xform_decls (ds : AST.declaration list) : AST.declaration list =
let replacer = new replaceArrayClass None in
let ds' = List.map (Isa_visitor.visit_decl (replacer :> Isa_visitor.isaVisitor)) ds in
let tuple_decls = replacer#mkTypeWrappers in
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record_decls, or simply:

Suggested change
let tuple_decls = replacer#mkTypeWrappers in
let decls = replacer#mkTypeWrappers in

Comment thread libISA/xform_arrays.ml
| Expr_ArrayInit (t, es) ->
let t' = Isa_visitor.visit_type (self :> Isa_visitor.isaVisitor) t in
let es' = Isa_visitor.visit_exprs (self :> Isa_visitor.isaVisitor) es in
let e' = AST.Expr_ArrayInit (t, es') in
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be t' I believe:

Suggested change
let e' = AST.Expr_ArrayInit (t, es') in
let e' = AST.Expr_ArrayInit (t', es') in

(looks like this is not covered by tests)


// Copyright (C) 2025-2025 Intel Corporation

var R : array [16] of Bits(32);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused.

Suggested change
var R : array [16] of Bits(32);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: returning arrays from functions generates hilariously bad C code

2 participants