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

Utilize the embedded literal value information in CodeType #3973

Open
Xinlu-Y opened this issue Oct 3, 2024 · 1 comment
Open

Utilize the embedded literal value information in CodeType #3973

Xinlu-Y opened this issue Oct 3, 2024 · 1 comment

Comments

@Xinlu-Y
Copy link
Collaborator

Xinlu-Y commented Oct 3, 2024

The initial goal was to optimize the GOOL by enhancing support for literal values and enabling more efficient operations, such as smartAdd and smartSub across different literal types (e.g., integers, floats, doubles, strings).

-- Binary Operators --
smartAdd :: (CommonRenderSym r) => SValue r -> SValue r -> SValue r
smartAdd v1 v2 = do
v1' <- v1
v2' <- v2
case (RC.valueInt v1', RC.valueInt v2') of
(Just i1, Just i2) -> litInt (i1 + i2)
_ -> v1 #+ v2
smartSub :: (CommonRenderSym r) => SValue r -> SValue r -> SValue r
smartSub v1 v2 = do
v1' <- v1
v2' <- v2
case (RC.valueInt v1', RC.valueInt v2') of
(Just i1, Just i2) -> litInt (i1 - i2)
_ -> v1 #- v2

  • Initial Approach
    Initially, we attempted to introduce a new data type LitValue and replaced valInt :: Maybe Integer with litVal :: LitValue. The purpose of LitValue was to capture a wider range of literal values. However, this approach proved to be too generic, resulting in a loss of type information. Details of this attempt are documented in Introduced LitValue for expanded literal value handling #3945.

  • Current Approach
    The current approach focuses on modifying the CodeType data structure to include an additional field to hold optional literal values (Maybe a). By adding this parameter, CodeType can store literals like Maybe Int, Maybe Double, or Maybe String, specific to each type.

    The updated definition for CodeType is as follows:

    data CodeType = Boolean (Maybe Bool)
                  | Integer (Maybe Int)
                  | Float (Maybe Float)
                  | Double (Maybe Double)
                  | Char (Maybe Char)
                  | String (Maybe String)
                  ...
    

    Since cType :: CodeType is a field within TypeData, this modification allows TypeData to carry literal value information as part of the type itself. This way, we can leverage the embedded literal values in CodeType for optimization in operations like smartAdd and smartSub.

  • Changes to valFromData
    With the new CodeType structure, the signature of valFromData was modified to include an additional Maybe a parameter that represents the literal value:
    valFromData :: Maybe Int -> Maybe a -> VSType r -> Doc -> SValue r

    • The main challenge was to properly insert the Maybe a value into CodeType, which already contains optional literal values for specific types. After that, the updated CodeType needed to be propagated into the TypeData and ValData structures effectively. This is where I am currently stuck.

Currently, functions like valueOf and inlineIf rely on valFromData by passing Nothing for the literal value parameter, as they don’t require this information to function correctly. This ensures compatibility with other parts of the code, such as listSlice.
Most of the time, the literal value information is not necessary except for certain optimizations like smartAdd and smartSub. Therefore, it’s important to consider if there are more straightforward ways to utilize these literal values effectively. For example, are there other operations or optimizations that could benefit from accessing literal values in CodeType?

@JacquesCarette
Copy link
Owner

I'm wondering if the problem has now moved to valFromData, i.e. it is general to too many types. I think maybe there should be different functions so that the new argument should be Maybe Bool for valFromBoolData, Maybe Int for valFromIntData, etc.

But maybe I misunderstand exactly where you are stuck in the propagation?

As to your last question: the main way for us to know how to use these more effectively is to look at Drasil-generated code and see if there are pieces of code that should have been simpler, i.e. where 'static computations' are present in the generated code.

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

No branches or pull requests

2 participants