Skip to content

Support for SSA Transformation in Chiron and Sparse Simple Constant Propagation#20

Open
sarthak-motwani wants to merge 2 commits into
PRAISE-group:cs335_ssa_sccpfrom
sarthak-motwani:master
Open

Support for SSA Transformation in Chiron and Sparse Simple Constant Propagation#20
sarthak-motwani wants to merge 2 commits into
PRAISE-group:cs335_ssa_sccpfrom
sarthak-motwani:master

Conversation

@sarthak-motwani

@sarthak-motwani sarthak-motwani commented Apr 24, 2025

Copy link
Copy Markdown

Pull Request Template for Feature Additions.

Brief description feature

Provide the details of the feature, explain it's functionality.

We have implemented Static Single Assignment (SSA) on Chiron IR, along with out-of-SSA. Additionally, we have implemented Sparse Simple Constant Propagation (SSCP) on the SSA Transformed IR. This optimization is completely optional and we have made sure that the user can either run only (SSA + out-of-SSA) or (SSA + SSCP + out-of-SSA).

Example

Give a test case and related commands that show the utility of the feature.

Running an example

Note: Use the --ir flag to see the new IR printed in the terminal. Also, use the -cfg_gen and -cfg_dump flags to dump the CFG for the original IR.

  • To perform SSA transformation, use the -ssa flag. (Note: If you are using the -r flag (also runs the program), the program may throw an error
    at a phi-instruction since it cannot be interpreted. So, you can run the program without -r flag).

  • To perform out-of-SSA transformation (after SSA transformation), use -outssa flag with the -ssa flag.

  • To perform SSCP optimization (after SSA transformation), use -ssa -sscp and -outssa flags.

The testcases are present in the demo_testcases directory within ChironCore. More examples are present in the example directory.

Testing SSA Transformation

  • The new IR will be printed on the terminal.
  • cfg0.png shows the CFG before transformation.
  • cfg2_old_after_rename.png shows the CFG after SSA renaming (before IR updates).
  • cfg3_new_post_ssa.png shows the CFG rebuilt from the SSA-transformed IR.

Testing Out-of-SSA Tranformation

  • The new IR will be printed on the terminal.
  • cfg4_old_out_of_ssa.png shows the CFG after Out-of-SSA transformation (before IR updates).
  • cfg5_new_out_of_ssa.png shows the CFG rebuilt from the Out-of-SSA transformed IR.
  • All other CFGs have the same description as in SSA transformation

Testing SSCP Optimization

  • The new IR (with variables replaced by corresponding constants) is printed on the terminal.
  • Lattice Values of all variables are printed on the terminal
  • cfg6_new_sscp.png shows the CFG after SSCP optimization, but before out-of-SSA
  • All other CFGs have the same description as in SSA and out of SSA transformation

Running the program with (SSA + out-of-SSA + with or without SSCP) and without any transformation should produce identical outputs.

$ cd ChironCore
$ ./chiron.py -r ./demo_testcases/4_nestedloops.tl -cfg_gen -cfg_dump --ir -ssa -outssa -sscp

The below example is shown for clarity. If the user wants to test a complex and large CFG with multiple levels of nesting on loops and if-else, please test 7_verybigprogram.tl within demo_testcases. There are other large examples within example folder as well.

Example 1: Showing SSA and out-of-SSA:

:x = 0
:y = 100
:z = 39
repeat 3 [ 
  if (:x < :y) [ 
    :uninit = 10
    :x = :x + 2
    if (:x - 2 == 0) [
      forward :x
    ] else [
      backward :y
      :t = 90
      left :t
      :y = :y - 1
    ]
    right 90
    backward :x
  ]
  forward :uninit  
  ]
penup
goto (:x, :y)
pendown
repeat 4[
    forward :z
    right 90
]

CFG Before SSA

cfg0_simple

CFG After SSA

cfg3_new_post_ssa

IR After SSA

 [L0] :x_1 = 0.0 [1]
 [L1] :y_1 = 100.0 [1]
 [L2] :z_1 = 39.0 [1]
 [L3] :__rep_counter_1_1 = 3.0 [1]
 [L4] :y_2 = φ(:y_1, :y_5) [1]
 [L5] :x_2 = φ(:x_1, :x_4) [1]
 [L6] :__rep_counter_1_2 = φ(:__rep_counter_1_1, :__rep_counter_1_3) [1]
 [L7] :uninit_1 = φ(:uninit_0, :uninit_3) [1]
 [L8] (:__rep_counter_1_2 > 0.0) [20]
 [L9] (:x_2 < :y_2) [13]
[L10] :uninit_2 = 10.0 [1]
[L11] :x_3 = (:x_2 + 2.0) [1]
[L12] ((:x_3 - 2.0) == 0.0) [3]
[L13] forward :x_3 [1]
[L14] False [5]
[L15] backward :y_2 [1]
[L16] :t_1 = 90.0 [1]
[L17] left :t_1 [1]
[L18] :y_3 = (:y_2 - 1.0) [1]
[L19] :y_4 = φ(:y_2, :y_3) [1]
[L20] right 90.0 [1]
[L21] backward :x_3 [1]
[L22] :y_5 = φ(:y_2, :y_4) [1]
[L23] :x_4 = φ(:x_2, :x_3) [1]
[L24] :uninit_3 = φ(:uninit_1, :uninit_2) [1]
[L25] forward :uninit_3 [1]
[L26] :__rep_counter_1_3 = (:__rep_counter_1_2 - 1.0) [1]
[L27] False [-23]
[L28] penup [1]
[L29] goto :x_2 :y_2 [1]
[L30] pendown [1]
[L31] :__rep_counter_2_1 = 4.0 [1]
[L32] :__rep_counter_2_2 = φ(:__rep_counter_2_1, :__rep_counter_2_3) [1]
[L33] (:__rep_counter_2_2 > 0.0) [5]
[L34] forward :z_1 [1]
[L35] right 90.0 [1]
[L36] :__rep_counter_2_3 = (:__rep_counter_2_2 - 1.0) [1]
[L37] False [-5]

CFG After out-of-SSA

cfg5_new_out_of_ssa

IR After out-of-SSA

 [L0] :x_1 = 0.0 [1]
 [L1] :y_1 = 100.0 [1]
 [L2] :z_1 = 39.0 [1]
 [L3] :__rep_counter_1_1 = 3.0 [1]
 [L4] :y_2 = :y_1 [1]
 [L5] :uninit_1 = 0.0 [1]
 [L6] :__rep_counter_1_2 = :__rep_counter_1_1 [1]
 [L7] :x_2 = :x_1 [1]
 [L8] (:__rep_counter_1_2 > 0.0) [25]
 [L9] (:x_2 < :y_2) [35]
[L10] :uninit_2 = 10.0 [1]
[L11] :x_3 = (:x_2 + 2.0) [1]
[L12] ((:x_3 - 2.0) == 0.0) [4]
[L13] forward :x_3 [1]
[L14] :y_4 = :y_2 [1]
[L15] False [6]
[L16] backward :y_2 [1]
[L17] :t_1 = 90.0 [1]
[L18] left :t_1 [1]
[L19] :y_3 = (:y_2 - 1.0) [1]
[L20] :y_4 = :y_3 [1]
[L21] right 90.0 [1]
[L22] backward :x_3 [1]
[L23] :y_5 = :y_4 [1]
[L24] :uninit_3 = :uninit_2 [1]
[L25] :x_4 = :x_3 [1]
[L26] forward :uninit_3 [1]
[L27] :__rep_counter_1_3 = (:__rep_counter_1_2 - 1.0) [1]
[L28] :y_2 = :y_5 [1]
[L29] :uninit_1 = :uninit_3 [1]
[L30] :__rep_counter_1_2 = :__rep_counter_1_3 [1]
[L31] :x_2 = :x_4 [1]
[L32] False [-24]
[L33] penup [1]
[L34] goto :x_2 :y_2 [1]
[L35] pendown [1]
[L36] :__rep_counter_2_1 = 4.0 [1]
[L37] :__rep_counter_2_2 = :__rep_counter_2_1 [1]
[L38] (:__rep_counter_2_2 > 0.0) [10]
[L39] forward :z_1 [1]
[L40] right 90.0 [1]
[L41] :__rep_counter_2_3 = (:__rep_counter_2_2 - 1.0) [1]
[L42] :__rep_counter_2_2 = :__rep_counter_2_3 [1]
[L43] False [-5]
[L44] :y_5 = :y_2 [1]
[L45] :uninit_3 = :uninit_1 [1]
[L46] :x_4 = :x_2 [1]
[L47] False [-21]

Output of the program before any transformation

image

Output of the program after SSA + out-of-SSA (Identical)

image

Example 2: SSA, out-of-SSA and SSCP optimization

:x = 5
:a = 10
:b = :a - 10
:c = 10
:d = 50
if (:a > :b) [
  if (:c < :d) [
    :x = :c * :d
    :c = :d
    :d = :c
    :c = :d
  ]
] else [
  :x = :a - :b
  :x = (:a + :b*:b + 3/2 + 8)/:a
]
:b = 5
repeat :d[
    forward 10/3
    :b = :b + 1/2
]
forward :x - 50

CFG (before any transformation or optimization)

cfg0_simple

CFG (After SSA)

cfg3_new_post_ssa

Lattice Values of all variables

:x_1 C(5.0)
:a_1 C(10.0)
:b_1 C(0.0)
:c_1 C(10.0)
:d_1 C(50.0)
:x_4 C(10.0)
:x_5 C(1.95)
:x_2 C(500.0)
:c_2 C(50.0)
:d_2 C(50.0)
:c_3 C(50.0)
:d_3 C(50.0)
:c_4 ⊥
:x_3 ⊥
:d_4 C(50.0)
:c_5 ⊥
:x_6 ⊥
:b_2 C(5.0)
:__rep_counter_1_1 C(50.0)
:b_4 ⊥
:__rep_counter_1_3 ⊥
:__rep_counter_1_2 ⊥
:b_3

CFG (After SSA + out-of-SSA + SSCP)

cfg5_new_out_of_ssa

IR (After SSA + out-of-SSA + SSCP)

[L0] :x_1 = 5.0 [1]
 [L1] :a_1 = 10.0 [1]
 [L2] :b_1 = 0.0 [1]
 [L3] :c_1 = 10.0 [1]
 [L4] :d_1 = 50.0 [1]
 [L5] (10.0 > 0.0) [12]
 [L6] (10.0 < 50.0) [29]
 [L7] :x_2 = 500.0 [1]
 [L8] :c_2 = 50.0 [1]
 [L9] :d_2 = 50.0 [1]
[L10] :c_3 = 50.0 [1]
[L11] :c_4 = 50.0 [1]
[L12] :x_3 = 500.0 [1]
[L13] :d_3 = 50.0 [1]
[L14] :c_5 = :c_4 [1]
[L15] :x_6 = :x_3 [1]
[L16] False [5]
[L17] :x_4 = 10.0 [1]
[L18] :x_5 = 1.95 [1]
[L19] :c_5 = 10.0 [1]
[L20] :x_6 = 1.95 [1]
[L21] :d_4 = 50.0 [1]
[L22] :b_2 = 5.0 [1]
[L23] :__rep_counter_1_1 = 50.0 [1]
[L24] :b_3 = 5.0 [1]
[L25] :__rep_counter_1_2 = 50.0 [1]
[L26] (:__rep_counter_1_2 > 0.0) [7]
[L27] forward (10.0 / 3.0) [1]
[L28] :b_4 = (:b_3 + (1.0 / 2.0)) [1]
[L29] :__rep_counter_1_3 = (:__rep_counter_1_2 - 1.0) [1]
[L30] :b_3 = :b_4 [1]
[L31] :__rep_counter_1_2 = :__rep_counter_1_3 [1]
[L32] False [-6]
[L33] forward (:x_6 - 50.0) [1]
[L34] False [4]
[L35] :c_4 = 10.0 [1]
[L36] :x_3 = 5.0 [1]
[L37] False [-24]

Please add a atleast 3 test cases to the repository which shows how the feature works and what are the outcomes.

The testcases are present in demo_testcases folder. More examples are also present in example folder.
Please note that for the 5_bigprogram_giveparams.tl, the parameters  `-d '{":x": 20, "y": 30, ":z": 20, ":p": 40}'` also need to be passed.

Why is the feature interesting?

Give use cases for the feature.

SSA transformation can be used to perform stronger sparse optimizations such as Sparse Conditional Constant Propagation, etc.

Screenshots

Please add screenshots whenever possible.

Added.

Other Details

Please add additional details that you want the developers to be aware of about the feature. Eg. good use cases (if any).

@sarthak-motwani sarthak-motwani changed the title ssa_outssa_sscp Support for SSA Transformation in Chiron IR (Along with out-of-SSA and (optional) Sparse Simple Constant Propagation) Apr 25, 2025
@lahiri-phdworks
lahiri-phdworks self-requested a review April 25, 2025 11:06
@lahiri-phdworks lahiri-phdworks added the enhancement New feature or request label Apr 25, 2025
@lahiri-phdworks

Copy link
Copy Markdown
Collaborator

@sarthak-motwani Thanks for the PR. I shall review it and add it to the branch.

@lahiri-phdworks
lahiri-phdworks changed the base branch from master to cs335_ssa_sccp April 25, 2025 11:08
@lahiri-phdworks lahiri-phdworks changed the title Support for SSA Transformation in Chiron IR (Along with out-of-SSA and (optional) Sparse Simple Constant Propagation) Support for SSA Transformation in Chiron IR and Sparse Simple Constant Propagation) Apr 25, 2025
@lahiri-phdworks lahiri-phdworks changed the title Support for SSA Transformation in Chiron IR and Sparse Simple Constant Propagation) Support for SSA Transformation in Chiron and Sparse Simple Constant Propagation Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug/Enhance]: Loop fails to terminate when loop argument is negative.

2 participants