GithubHelp home page GithubHelp logo

Implement G_ICMP/G_SELECT about llvm-mos HOT 6 CLOSED

llvm-mos avatar llvm-mos commented on July 20, 2024
Implement G_ICMP/G_SELECT

from llvm-mos.

Comments (6)

mysterymath avatar mysterymath commented on July 20, 2024 1

There's broadly two ways to deal with this: make the legalizer not emit this, and/or handle it when it does. It's unclear which is easier for this case; G_ICMP i16, i32, and i64 collectively have a metric ton of cases to handle, but G_SELECT is fairly difficult to lower, since it involves control flow. A good implementation of G_ICMP might also involve control flow, too, so that might not make too much difference.

Control flow is trickly to handle in the isel pipeline; pretty much all the passes naively iterate over the basic blocks and assume there are no changes to the set. The one exception I'm aware of is the FinalizeISel pass right after instruction selection, which has dedicated logic to lower pseudos that might introduce or remove basic blocks.

We do get some immediate benefit from the Legalizer handling all of this; it emits terrifically bad code, but it does bring the full set of cases down to 8-bits.

from llvm-mos.

mysterymath avatar mysterymath commented on July 20, 2024

Yanno, I took a closer look at the code in LegalizerHelper.cpp, and amazingly, this is actually one of the two standard implementations of 16-bit compares on the 6502!

The usual logic is: compare the high byte for equality, then if it's equal, do the comparison on the low byte, otherwise, do the comparison on the high byte. That's exactly what the Legalizer emits: a G_SELECT between the low comparison result and the high comparsion result, based on whether or not the high byte is equal.

Accordingly, if we go ahead and lower G_SELECT to a pseudo-instruction, then emit control flow for it, I think the natural 2-byte comparison sequence sort of just falls out. We'll probably want to eventually implement a combine to detect if there are any 16-bit subtractions with the same arguments; the other variant of 16-bit comparison is to do a 16-bit subtraction, which we may already be doing. In that case, we can tack on a pseudo or something to the subtractions that mark that the comparison result is needed. How to get that to work was a big open question in the earliest versions of my pre-LLVM code generation plans, but in GlobalISel, it just falls right out!

from llvm-mos.

johnwbyrd avatar johnwbyrd commented on July 20, 2024

An example of lowering G_SELECT to a pseudo, starts around 23:15:

https://www.youtube.com/watch?v=Zh4R40ZyJ2k

from llvm-mos.

johnwbyrd avatar johnwbyrd commented on July 20, 2024

See https://github.com/llvm-mos/llvm-mos/tree/bug/issue-40 for a failing G_ICMP/G_SELECT combination. Building the check-llvm-codegen-mos target reproduces.

from llvm-mos.

johnwbyrd avatar johnwbyrd commented on July 20, 2024

The game.ll example program in https://github.com/llvm-mos/llvm-mos/tree/bug/issue-40, was actually taken from https://github.com/sgadrat/6502-compilers-bench/blob/master/code_samples/ccgame/game_01_start.c . So an actual benchmark comparing llvm-mos against other C compilers will be possible after this bug is completed. I see frequent progress occuring on these issues, thank you.

from llvm-mos.

mysterymath avatar mysterymath commented on July 20, 2024

This should be more-or-less working; the printf.ll example I'm working off of fails in register allocation now, way down the pipe from instruction selection. You'll have to give the game example another try; chances are something else with it will break, but maybe not.

from llvm-mos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.