GithubHelp home page GithubHelp logo

Comments (1)

mewmew avatar mewmew commented on July 17, 2024

The specific ambiguity I'm trying to figure out how to deal with is described in llir/llvm#40.

I've tried quite a few different approaches to resolve the ambiguity by making changes to the grammar, but so far, I've been unsuccessful to find a working approach. Any feedback would be much appreciated, as my "usual" approaches don't seem to work this time.

The core issue boils down to the fact that LLVM IR has a lost of optional fields and attributes, as already discussed in other issues.

In particular, what I would like to do, is to enable Align in ReturnAttribute and FuncAttribute.

# NOTE: FuncAttribute should contain Align. However, using LALR(1) this
# produces a reduce/reduce conflict as the global attributes of
# GlobalDecl and GlobalDef also contains Align.

%interface FuncAttribute;

# TODO: Figure out how to enable Align in FuncAttribute again.

FuncAttribute -> FuncAttribute
	: AttrString
	| AttrPair
	# not used in attribute groups.
	| AttrGroupID
	# used in functions.
	#| Align # NOTE: removed to resolve reduce/reduce conflict, see above.
	# used in attribute groups.
	| AlignPair
	| AlignStack
	| AlignStackPair
	| AllocSize
	| FuncAttr
;

ReturnAttribute -> ReturnAttribute
	# TODO: Figure out how to re-enable without getting these errors in FuncHeader:
	#    - two unnamed fields share the same type `AttrPair`: ReturnAttribute -vs- FuncAttribute
	#    - `AttrPair` occurs in both named and unnamed fields
	#    - `ReturnAttrs` cannot be nullable, since it precedes FuncAttrs
	#: AttrString
	#| AttrPair
	#| Align
	: Dereferenceable
	| ReturnAttr
;

The GlobalDecl production rule optionally contains both Align (as a comma separated global attribute (',' Align)?) and FuncAttribute and is defined as follows:

GlobalDecl -> GlobalDecl
	: Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? (',' Align)? Metadata=(',' MetadataAttachment)+? FuncAttrs=(',' FuncAttribute)+?
;

And, Align is simply defined as:

Align -> Align
	: 'align' N=UintLit
;

Initially when I try to update the grammar I get ll.tm,816: Align occurs in both named and unnamed fields. This is easily resolved by assigning a name; i.e. Align=(',' Align)?.

At this point, I get the error ll.tm,816: Align cannot be nullable, since it precedes FuncAttrs which seem to be related to #25.

Now, this is usually where I try to break up use of optional non-terminals in production rules into 2^n alternatives, where n is the number of optional non-terminals that are part of the conflict.

While not pretty, the following approach was used successfully with Gocc:

From ll.bnf:

GlobalDecl
	: GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType OptCommaAttachedMDList                                    << astx.NewGlobalDecl($0, $3, $4, $5, $6) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Align OptCommaAttachedMDList                          << astx.NewGlobalDecl($0, $3, $4, $5, $8) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Comdat OptCommaAttachedMDList                         << astx.NewGlobalDecl($0, $3, $4, $5, $8) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Comdat "," Align OptCommaAttachedMDList               << astx.NewGlobalDecl($0, $3, $4, $5, $10) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Section OptCommaAttachedMDList                        << astx.NewGlobalDecl($0, $3, $4, $5, $8) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Section "," Align OptCommaAttachedMDList              << astx.NewGlobalDecl($0, $3, $4, $5, $10) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Section "," Comdat OptCommaAttachedMDList             << astx.NewGlobalDecl($0, $3, $4, $5, $10) >>
	| GlobalIdent "=" ExternLinkage GlobalOptions Immutable ConcreteType "," Section "," Comdat "," Align OptCommaAttachedMDList   << astx.NewGlobalDecl($0, $3, $4, $5, $12) >>
;

I've tried to apply similar techniques using Textmapper, but so far to no vail.

This is what I tried:

GlobalDecl -> GlobalDecl
	: Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)?
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? FuncAttrs=(',' FuncAttribute)+
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? Metadata=(',' MetadataAttachment)+
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? Metadata=(',' MetadataAttachment)+ FuncAttrs=(',' FuncAttribute)+
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? Align=(',' Align)+
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? Align=(',' Align)+ FuncAttrs=(',' FuncAttribute)+
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? Align=(',' Align)+ Metadata=(',' MetadataAttachment)+
	| Name=GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable ContentType=Type (',' Section)? (',' Comdat)? Align=(',' Align) Metadata=(',' MetadataAttachment)+ FuncAttrs=(',' FuncAttribute)+
;

Which results in the following error:

ll.tm,816: `Align` cannot be nullable, since it precedes FuncAttrs
ll.tm,829: `Align` occurs in both named and unnamed fields
ll.tm,820: input: TopLevelEntity_optlist GlobalIdent '=' ExternLinkage Preemptionopt Visibilityopt DLLStorageClassopt ThreadLocalopt UnnamedAddropt AddrSpaceopt ExternallyInitializedopt Immutable Type ',' Align
reduce/reduce conflict (next: eoi, global_ident_tok, local_ident_tok, comdat_name_tok, metadata_name_tok, metadata_id_tok, 'attributes', 'declare', 'define', 'module', 'source_filename', 'target', 'uselistorder_bb', 'uselistorder')
    list_of_','_and_1_elements2 : ',' Align
    FuncAttribute : Align

conflicts: 8 shift/reduce and 9 reduce/reduce
lalr: 0.332s, text: 0.592s, parser: 2228 states, 229KB

Any thoughts? Is there something obvious I'm missing, or is this grammar especially tricky since it contains so many optional attributes?

Kindly,
Robin

from textmapper.

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.