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

C++ Formatting + Macro Parsing Error #158

Open
asglover opened this issue Oct 24, 2024 · 1 comment
Open

C++ Formatting + Macro Parsing Error #158

asglover opened this issue Oct 24, 2024 · 1 comment

Comments

@asglover
Copy link

I've got a file that I'm editing as with jinja-cpp formatter.

I import a macro from another file with:

{%- from 'macros.jinja' import declare_smem_arrays with context %}

The macro in its entirety is:

{#
First input argument consists of a dictionary with keys _common_ and _per_warp_.
Keys map to lists of tuples with (name, dtype, num_elements) of each subarray.
#}
{%- macro declare_smem_arrays(arrays, warp_loc_var, config) %}
    {%- set warps_per_block = divide(config.num_threads, config.warp_size) %}
    extern __shared__ char s[];
    {%- set ns = {"offset": 0, "total_warp_bytes": 0} %}

    {%- for name, dtype, num_elements in arrays["common"] %}
        {{dtype}}* {{name}} = ({{dtype}}*) (s + {{ ns["offset"] }});
        {% do ns.update({"offset": ns["offset"] + num_elements * sizeof(dtype)}) %}
        {% if ns["offset"] > config.smem %}
            {{ raise("Error, required shared memory exceeds allocation maximum!") }}
        {% endif %}
    {%- endfor %}

    {%- for name, dtype, num_elements in arrays["per_warp"] %}
        {% do ns.update({"total_warp_bytes": ns["total_warp_bytes"] + num_elements * sizeof(dtype)}) %}
    {%- endfor %}

    {%- if ns["offset"] + ns["total_warp_bytes"] * warps_per_block > config.smem %}
        {{ raise("Error, required shared memory exceeds allocation maximum!") }}
    {%- endif %}

    char* per_warp_smem = s + {{ns["offset"]}} + {{ns["total_warp_bytes"]}} * {{ warp_loc_var }};

    {%- do ns.update({"offset": 0}) %}
    {%- for name, dtype, num_elements in arrays["per_warp"] %}
        {{dtype}}* {{name}} = ({{dtype}}*) (per_warp_smem + {{ ns["offset"] }});
        {% do ns.update({"offset": ns["offset"] + num_elements * sizeof(dtype)}) %}
    {%- endfor %}
{%- endmacro %}

When I use the macro, later in the file, something goes wrong and it views the rest of the file as a comment or invalid.
This is not actually a performance issue, the template and macro works perfectly. It's just the visiualization / highlighting that is broken.

image

I've included the text that causes the error.

    {{ declare_smem_arrays( { 
        "common": [], 
        "per_warp": [
            ("L1_smem", "float", L1.rep_len),
            ("L2_smem", "float", L2.rep_len), 
            ("L3_smem", "float", L3.rep_len)
            ]}, "warp_loc", forward_config)}}

    OTHER STUFF 

Please let me know if there's something I should be doing differently with whitespace or something to solve this.
I tried putting it all on one line and it still would not parse, so I don't think it's related to the line breaks.
If there are any logs or anything I can include to help, let me know, thank you!

@asglover
Copy link
Author

Again, thanks for the software, it rules in general!

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

1 participant