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

Support --no-loc to skip location info in Json #2709

Merged
merged 3 commits into from
Oct 25, 2023

Conversation

Shaikh-Ubaid
Copy link
Member

@Shaikh-Ubaid Shaikh-Ubaid commented Oct 24, 2023

This PR supports a --no-loc flag which enables the user to skip the location information in the generated AST/R Json format.

$ lfortran examples/expr2.f90 --show-asr --json --no-loc
{
    "node": "TranslationUnit",
    "fields": {
        "symtab": {
            "node": "SymbolTable1",
            "fields": {
                "expr2": {
                    "node": "Program",
                    "fields": {
                        "symtab": {
                            "node": "SymbolTable2",
                            "fields": {
                                "x": {
                                    "node": "Variable",
                                    "fields": {
                                        "parent_symtab": 2,
                                        "name": "x",
                                        "dependencies": [],
                                        "intent": "Local",
                                        "symbolic_value": [],
                                        "value": [],
                                        "storage": "Default",
                                        "type": {
                                            "node": "Integer",
                                            "fields": {
                                                "kind": 4
                                            }
                                        },
                                        "type_declaration": [],
                                        "abi": "Source",
                                        "access": "Public",
                                        "presence": "Required",
                                        "value_attr": false
                                    }
                                }
                            }
                        },
                        "name": "expr2",
                        "dependencies": [],
                        "body": [
                            {
                                "node": "Assignment",
                                "fields": {
                                    "target": {
                                        "node": "Var",
                                        "fields": {
                                            "v": "x (SymbolTable2)"
                                        }
                                    },
                                    "value": {
                                        "node": "IntegerBinOp",
                                        "fields": {
                                            "left": {
                                                "node": "IntegerBinOp",
                                                "fields": {
                                                    "left": {
                                                        "node": "IntegerConstant",
                                                        "fields": {
                                                            "n": 2,
                                                            "type": {
                                                                "node": "Integer",
                                                                "fields": {
                                                                    "kind": 4
                                                                }
                                                            }
                                                        }
                                                    },
                                                    "op": "Add",
                                                    "right": {
                                                        "node": "IntegerConstant",
                                                        "fields": {
                                                            "n": 3,
                                                            "type": {
                                                                "node": "Integer",
                                                                "fields": {
                                                                    "kind": 4
                                                                }
                                                            }
                                                        }
                                                    },
                                                    "type": {
                                                        "node": "Integer",
                                                        "fields": {
                                                            "kind": 4
                                                        }
                                                    },
                                                    "value": {
                                                        "node": "IntegerConstant",
                                                        "fields": {
                                                            "n": 5,
                                                            "type": {
                                                                "node": "Integer",
                                                                "fields": {
                                                                    "kind": 4
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            },
                                            "op": "Mul",
                                            "right": {
                                                "node": "IntegerConstant",
                                                "fields": {
                                                    "n": 5,
                                                    "type": {
                                                        "node": "Integer",
                                                        "fields": {
                                                            "kind": 4
                                                        }
                                                    }
                                                }
                                            },
                                            "type": {
                                                "node": "Integer",
                                                "fields": {
                                                    "kind": 4
                                                }
                                            },
                                            "value": {
                                                "node": "IntegerConstant",
                                                "fields": {
                                                    "n": 25,
                                                    "type": {
                                                        "node": "Integer",
                                                        "fields": {
                                                            "kind": 4
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "overloaded": []
                                }
                            },
                            {
                                "node": "Print",
                                "fields": {
                                    "values": [
                                        {
                                            "node": "Var",
                                            "fields": {
                                                "v": "x (SymbolTable2)"
                                            }
                                        }
                                    ],
                                    "separator": [],
                                    "end": []
                                }
                            }
                        ]
                    }
                }
            }
        },
        "items": []
    }
}

@Shaikh-Ubaid
Copy link
Member Author

We have the flag --dump-all-passes, but it only prints the asr in text format. I often use fortran, the json and the visualisation format to debug codes. This PR supports applying all passes to fortran, json, viz and tree formats.

@@ -2058,6 +2063,7 @@ int main(int argc, char *argv[])
app.add_flag("--legacy-array-sections", compiler_options.legacy_array_sections, "Enables passing array items as sections if required");
app.add_flag("--ignore-pragma", compiler_options.ignore_pragma, "Ignores all the pragmas");
app.add_flag("--stack-arrays", compiler_options.stack_arrays, "Allocate memory for arrays on stack");
app.add_flag("--all-passes", compiler_options.all_passes, "Apply all ASR passes to --show-asr and --show-fortran");
Copy link
Contributor

@certik certik Oct 24, 2023

Choose a reason for hiding this comment

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

Why not adding an option like --dump-fortran (or something like that) to dump Fortran together with Clojure in --dump-all-passes.

The same with JSON and --visualize.

@certik
Copy link
Contributor

certik commented Oct 24, 2023

How about this:

  • --show-asr is telling LFortran to print ASR as Clojure like output, which is exactly 1:1 with the internal ASR representation in memory
  • --show-fortran is telling LFortran to print ASR as Fortran code (directly)
  • --show-python (TODO) will print LPython code
  • --json will print ASR as JSON
  • --tree
  • --visualize

These (and more!) are various options that control how ASR is printed.

Then we have options that control which exact ASR gets printed:

By default the above prints ASR before any passes are applied.

Then --dump-all-passes will print the above for all passes.

Let's start with the above. If we need to, we can later add --last-pass to print the ASR after the last pass.

We also have options to control which passes to run, which would affect --dump-all-passes.

@Thirumalai-Shaktivel
Copy link
Member

@Shaikh-Ubaid I thought of working on --dump-fortran; did you start working on it?

@Shaikh-Ubaid
Copy link
Member Author

Shaikh-Ubaid commented Oct 25, 2023

@Shaikh-Ubaid I thought of working on --dump-fortran; did you start working on it?

I will take care of these Thirumalai. There are tons of other tasks waiting to be fixed. Please focus on them.

@Shaikh-Ubaid Shaikh-Ubaid force-pushed the add_flags_all_passes_and_no_loc branch from a11c1a8 to ba03b81 Compare October 25, 2023 10:21
@Shaikh-Ubaid Shaikh-Ubaid changed the title Add --all-passes and --no-loc flags Support --no-loc to skip location info in Json Oct 25, 2023
@Shaikh-Ubaid
Copy link
Member Author

I updated the PR description. This is ready. Please let me know if a reference test is to be added. I feel it is not needed. (Or can be added later if needed).

std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules=false);
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules=false);
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool no_loc=false, bool show_intrinsic_modules=false);
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool no_loc=false, bool show_intrinsic_modules=false);
Copy link
Contributor

@certik certik Oct 25, 2023

Choose a reason for hiding this comment

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

Let's not use default arguments, it leads to issues with overloads and clarity of what the values actually are. For example, if there is any code in LFortran that just called pickle_json(asr, lm, true), then after this PR this very same code now will evaluate to no_loc=true and show_intrinsic_modules=false, which is not what was intended.

To fix this, do it like this:

Suggested change
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool no_loc=false, bool show_intrinsic_modules=false);
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool no_loc, bool show_intrinsic_modules);

And then modify all call sites to supply all arguments explicitly.

std::string pickle_json(AST::ast_t &ast, LocationManager &lm);
std::string pickle_json(AST::TranslationUnit_t &ast, LocationManager &lm);
std::string pickle_json(AST::ast_t &ast, LocationManager &lm, bool no_loc=false);
std::string pickle_json(AST::TranslationUnit_t &ast, LocationManager &lm, bool no_loc=false);
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, no default arguments.

@certik certik marked this pull request as draft October 25, 2023 20:36
@certik
Copy link
Contributor

certik commented Oct 25, 2023

Otherwise it looks good.

@Shaikh-Ubaid Shaikh-Ubaid force-pushed the add_flags_all_passes_and_no_loc branch from ba03b81 to a9d8cbc Compare October 25, 2023 20:54
@Shaikh-Ubaid Shaikh-Ubaid force-pushed the add_flags_all_passes_and_no_loc branch from a9d8cbc to 698cdfa Compare October 25, 2023 21:04
@Shaikh-Ubaid Shaikh-Ubaid marked this pull request as ready for review October 25, 2023 21:04
@certik certik merged commit 176d878 into lfortran:main Oct 25, 2023
20 checks passed
@Shaikh-Ubaid Shaikh-Ubaid deleted the add_flags_all_passes_and_no_loc branch October 25, 2023 22:18
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.

None yet

3 participants