Skip to content
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

Add a new fusion instruction and remove unnecessary ones #227

Merged
merged 2 commits into from
Sep 25, 2023

Conversation

qwe661234
Copy link
Collaborator

After constant propagation and constant folding, we can optimize some ALU or brnach instructions into simple instructions.

Close: #226

src/emulate.c Dismissed Show dismissed Hide dismissed
src/emulate.c Fixed Show fixed Hide fixed
src/emulate.c Outdated Show resolved Hide resolved
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Consistently employ the prefix constopt_ rather than const_opt to prevent potential confusion.

src/const_opt.c Outdated Show resolved Hide resolved
src/emulate.c Outdated Show resolved Hide resolved
@jserv
Copy link
Contributor

jserv commented Sep 24, 2023

Both constant folding and constant propagation are related compiler optimization techniques. Constant propagation replaces the bound variable with the constant expression it is bound to. Conversely, constant folding evaluates the expression with all compile-time inputs. I am a bit confused about the proposed changes, as I cannot ensure that a fully functional implementation exists for either constant folding or constant propagation. Can you verify this?

src/emulate.c Outdated Show resolved Hide resolved
src/emulate.c Outdated Show resolved Hide resolved
src/emulate.c Outdated Show resolved Hide resolved
With constant propagation and constant folding, we can optimize some ALU
or brnach instructions into simple instructions.

See: sysprog21#226
src/emulate.c Dismissed Show dismissed Hide dismissed
src/emulate.c Outdated Show resolved Hide resolved
src/emulate.c Outdated Show resolved Hide resolved
@qwe661234
Copy link
Collaborator Author

Both constant folding and constant propagation are related compiler optimization techniques. Constant propagation replaces the bound variable with the constant expression it is bound to. Conversely, constant folding evaluates the expression with all compile-time inputs. I am a bit confused about the proposed changes, as I cannot ensure that a fully functional implementation exists for either constant folding or constant propagation. Can you verify this?

There is a simple example in ./build/dhrystone.elf can be optimized by constant folding and constant propagation.

  • Before
   0x100f0:	auipc	gp,0x18
   0x100f4:	add	gp,gp,864 
   0x100f8:	add	a0,gp,-1996
   0x100fc:	auipc	a2,0x1a
   0x10100:	add	a2,a2,1384 #
   0x10104:	sub	a2,a2,a0
  • After
   0x100f0:	lui	gp,  0x10108        
   0x100f4:	lui	gp,  0x10468
   0x100f8:	lui	a0, 0xFC9C
   0x100fc:	lui	a2,0x10116
   0x10100:	lui	a2, 0x1067E 
   0x10104:	lui    a2, 0x9e2

Moreover, these 6 lui instructions can be fused into a fused instruction.

@jserv
Copy link
Contributor

jserv commented Sep 25, 2023

There is a simple example in build/dhrystone.elf can be optimized by constant folding and constant propagation.
[...]
Moreover, these 6 lui instructions can be fused into a fused instruction.

Describe this within git commit messages.

src/emulate.c Show resolved Hide resolved
src/emulate.c Outdated Show resolved Hide resolved
After constant propagation and constant folding, auipc + addi and lui +
addi become 2 lui instuctions. Moreover, we discover a sequence of lui
instrution existing in optimized basic block, so we add a new fusion
function to handle multiple lui instructions.

There is a simple example in ./build/dhrystone.elf can be optimized by
constant folding and constant propagation.

Before
   0x100f0: auipc gp,0x18
   0x100f4: add gp,gp,864
   0x100f8: add a0,gp,-1996
   0x100fc: auipc a2,0x1a
   0x10100: add a2,a2,1384
   0x10104: sub a2,a2,a0
After
   0x100f0: lui gp, 0x10108
   0x100f4: lui gp, 0x10468
   0x100f8: lui a0, 0xFC9C
   0x100fc: lui a2,0x10116
   0x10100: lui a2, 0x1067E
   0x10104: lui a2, 0x9e2

Moreover, these 6 lui instructions can be fused into a fused instruction.
@jserv jserv merged commit 9f63962 into sysprog21:master Sep 25, 2023
14 checks passed
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.

Constant propagation and constant folding
2 participants