Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

What is the Correct Way to Add Data Member in User Defined Dialect Type? #29

Closed

Description

@yurikamome

Hello,

I am trying to creating my own dialect with a new dialect type, say ToyArrayType, according to your toy example. My ToyArraytype contains an extra member data, a "string".

I want the "string" go along with me during the lowering to my Toy MLIR. However it seems that the ToyArrayType will be cast to mlir::Type during the lowering, and that makes me lost the "string" data because mlir::Type doesn't have that member.

For example, in Ch5/ToyDialect.cpp, function ReshapeOp::build(...), (around line 355), the statement:
state->types.push_back(ToyArrayType::get(builder->getContext()));

It seems that if the ToyArrayType has some member data, the "string", it will lose it when being pushed into the "state->types", which is a SmallVectormlir::Type.

If I am not on the correct path, could you please let me know the best way to add a data member in user dialect type? (without change/hacking codes for mlir::Type).

Thanks very much,
Rui

Activity

  1. joker-eph commented on May 29, 2019

    @joker-eph
    Contributor
  2. yurikamome commented on May 29, 2019

    @yurikamome
    Author

    Hello all,

    Thanks for all the answers a lot. But it seems that I still have the issue with dyn_cast<>.

    I have a non-empty data member: vector in ToyArray. The ToyArray is assigned to a mlir::Type, and then dyn_cast<> back to a ToyArray, at that time the vector data member becomes empty.

    I attached an example code. Could you please correct me if the example is written in the wrong way.

    class ToyArrayType has a public data member: vector _hello_world.

    At Ch5/ToyDialect.cpp, around line 354,

    void ReshapeOp::build(mlir::Builder *builder, mlir::OperationState *state,
    mlir::Value *value, ToyArrayType reshapedType) {
    //init _hello_world data for reshapedType
    reshapedType._hello_world = vector({"Hello","World"});

      //push to the operation state
      state->types.push_back(reshapedType); 
     
      //print size of _hello_world, it prints "2"
      llvm::errs()<< reshapedType._hello_world.size() ;  
    
      //Now take back the mlir::Type from the state
      mlir::Type base_type = state->types[0];
    
      //dynamic cast to ToyArrayType
      ToyArrayType toy_type = base_type.dyn_cast<ToyArrayType>();
       
      //print _hello_world.size().  It prints "0",  but I expect it to be "2"
      llvm::errs()<<toy_type._hello_world.size();
    

    }

    Thanks,
    Rui

  3. River707 commented on May 29, 2019

    @River707
    Contributor

    It seems like you are putting the field directly on the ToyArrayType class. Types, and many other things in MLIR, are value typed(passed by value). As such, they are essentially just smart pointers to an internal storage class(this is detailed a bit more in the link I posted). To add a field to a Type class you have to add to its storage class:
    https://github.com/tensorflow/mlir/blob/master/examples/toy/Ch5/mlir/ToyDialect.cpp#L45

  4. yurikamome commented on May 29, 2019

    @yurikamome
    Author

    Thanks! The way of putting fields into Storage class works! Thanks to all the answers again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions