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

Where do we get canInferType from the json? #2

Open
jumerckx opened this issue Dec 17, 2024 · 0 comments
Open

Where do we get canInferType from the json? #2

jumerckx opened this issue Dec 17, 2024 · 0 comments

Comments

@jumerckx
Copy link

The old jl-generators.cc calls canInferType which is defined as:

  static bool canInferType(const Operator &op)
  {
    return hasSameArgumentAndResultTypes(op) ||
           hasFirstAttrDerivedResultTypes(op) || hasInferTypeInterface(op);
  }

with these functions:

  static bool hasSameArgumentAndResultTypes(const Operator &op)
  {
    return op.getTrait("::mlir::OpTrait::SameOperandsAndResultType") &&
           op.getNumVariableLengthResults() == 0;
  }

  static bool hasFirstAttrDerivedResultTypes(const Operator &op)
  {
    return op.getTrait("::mlir::OpTrait::FirstAttrDerivedResultType") &&
           op.getNumVariableLengthResults() == 0;
  }

  static bool hasInferTypeInterface(const Operator &op)
  {
    return op.getTrait("::mlir::InferTypeOpInterface::Trait") &&
           op.getNumRegions() == 0;
  }

It is not clear to me how to do this using Tablegen's json backend.
To debug, I'm looking at gpu.block_dim for which the old generator generates the code for result inference:
result_inference=(length(_results) == 0 ? true : false).
The json for this operation does not contain any clear indication of any of these traits

json (only the operation itself)
"GPU_BlockDimOp": {
    "!anonymous": false,
    "!fields": [],
    "!locs": [
        "GPUOps.td:155"
    ],
    "!name": "GPU_BlockDimOp",
    "!superclasses": [
        "Op",
        "GPU_Op",
        "Arguments",
        "Results",
        "GPU_IndexOp"
    ],
    "arguments": {
        "args": [
            [
                {
                    "def": "GPU_DimensionAttr",
                    "kind": "def",
                    "printable": "GPU_DimensionAttr"
                },
                "dimension"
            ],
            [
                {
                    "def": "anonymous_547",
                    "kind": "def",
                    "printable": "anonymous_547"
                },
                "upper_bound"
            ]
        ],
        "kind": "dag",
        "operator": {
            "def": "ins",
            "kind": "def",
            "printable": "ins"
        },
        "printable": "(ins GPU_DimensionAttr:$dimension, anonymous_547:$upper_bound)"
    },
    "assemblyFormat": "$dimension (`upper_bound` $upper_bound^)? attr-dict",
    "builders": [
        {
            "def": "anonymous_548",
            "kind": "def",
            "printable": "anonymous_548"
        },
        {
            "def": "anonymous_549",
            "kind": "def",
            "printable": "anonymous_549"
        }
    ],
    "cppNamespace": "::mlir::gpu",
    "description": "\n    Returns the number of threads in the thread block (aka the block size) along\n    the x, y, or z `dimension`.\n\n    Example:\n\n    ```mlir\n    %bDimX = gpu.block_dim x\n    ```\n\n    If `known_block_size` is set on an this operation's enclosing `gpu.func`,\n    or `gpu.known_block_size` is set on an enclosing `FunctionOpInterface`\n    implementor, or if the enclosing `gpu.launch` specifies a constant size for\n    `dimension`'s blocks, these contextual facts may be used to infer that this\n    operation has a constant value, though such a transformation will not be\n    performed by canonicalization or the default constant folder. Executions which\n    cause that constant-value assumption to be false incur undefined behavior.\n\n    If `upper_bound` is set, executions where the bblock size along `dimension`\n    exceeds `upper_bound` cause undefined behavior.\n\n    There is an implicit upper bound of `kMaxDim` (currently uint32_t::max).\n  ",
    "extraClassDeclaration": null,
    "extraClassDefinition": "\n    void $cppClass::getAsmResultNames(\n        llvm::function_ref<void(mlir::Value, mlir::StringRef)> setNameFn) {\n      auto dimStr = stringifyDimension(getDimensionAttr().getValue());\n      auto opName = getOperationName();\n      opName.consume_front(\"gpu.\");\n      SmallString<8> resultName({opName, \"_\", dimStr});\n      setNameFn(getResult(),resultName);\n    }\n  ",
    "hasCanonicalizeMethod": 0,
    "hasCanonicalizer": 0,
    "hasCustomAssemblyFormat": 0,
    "hasFolder": 0,
    "hasRegionVerifier": 0,
    "hasVerifier": 0,
    "opDialect": {
        "def": "GPU_Dialect",
        "kind": "def",
        "printable": "GPU_Dialect"
    },
    "opDocGroup": null,
    "opName": "block_dim",
    "regions": {
        "args": [],
        "kind": "dag",
        "operator": {
            "def": "region",
            "kind": "def",
            "printable": "region"
        },
        "printable": "(region)"
    },
    "results": {
        "args": [
            [
                {
                    "def": "Index",
                    "kind": "def",
                    "printable": "Index"
                },
                null
            ]
        ],
        "kind": "dag",
        "operator": {
            "def": "outs",
            "kind": "def",
            "printable": "outs"
        },
        "printable": "(outs Index)"
    },
    "skipDefaultBuilders": 0,
    "successors": {
        "args": [],
        "kind": "dag",
        "operator": {
            "def": "successor",
            "kind": "def",
            "printable": "successor"
        },
        "printable": "(successor)"
    },
    "summary": "",
    "traits": [
        {
            "def": "Pure",
            "kind": "def",
            "printable": "Pure"
        },
        {
            "def": "anonymous_545",
            "kind": "def",
            "printable": "anonymous_545"
        },
        {
            "def": "anonymous_546",
            "kind": "def",
            "printable": "anonymous_546"
        }
    ],
    "useCustomPropertiesEncoding": 0
}
Recursing through the traits field isn't the right approach either.

I'm not even sure which trait is being picked up in the old generator, as I don't find any of SameOperandsAndResultType, FirstAttrDerivedResultType, or InferTypeOpInterface in the tablegen sources for this operation...

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