diff --git a/mlir/docs/CAPI.md b/mlir/docs/CAPI.md --- a/mlir/docs/CAPI.md +++ b/mlir/docs/CAPI.md @@ -89,8 +89,11 @@ For indexed components, the following pair of functions is provided. -- `unsigned mlirXGetNums(MlirX)` returns the upper bound on the index. -- `MlirY mlirXGet(MlirX, unsigned pos)` returns 'pos'-th subobject. +- `intptr_t mlirXGetNums(MlirX)` returns the upper bound on the index. +- `MlirY mlirXGet(MlirX, intptr_t pos)` returns 'pos'-th subobject. + +The sizes are accepted and returned as signed pointer-sized integers, i.e. +`intptr_t`. This typedef is avalable in C99. Note that the name of subobject in the function does not necessarily match the type of the subobject. For example, `mlirOperationGetOperand` returns a diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -18,6 +18,8 @@ #ifndef MLIR_C_IR_H #define MLIR_C_IR_H +#include + #ifdef __cplusplus extern "C" { #endif @@ -122,15 +124,15 @@ struct MlirOperationState { const char *name; MlirLocation location; - unsigned nResults; + intptr_t nResults; MlirType *results; - unsigned nOperands; + intptr_t nOperands; MlirValue *operands; - unsigned nRegions; + intptr_t nRegions; MlirRegion *regions; - unsigned nSuccessors; + intptr_t nSuccessors; MlirBlock *successors; - unsigned nAttributes; + intptr_t nAttributes; MlirNamedAttribute *attributes; }; typedef struct MlirOperationState MlirOperationState; @@ -139,15 +141,15 @@ MlirOperationState mlirOperationStateGet(const char *name, MlirLocation loc); /** Adds a list of components to the operation state. */ -void mlirOperationStateAddResults(MlirOperationState *state, unsigned n, +void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, MlirType *results); -void mlirOperationStateAddOperands(MlirOperationState *state, unsigned n, +void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue *operands); -void mlirOperationStateAddOwnedRegions(MlirOperationState *state, unsigned n, +void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion *regions); -void mlirOperationStateAddSuccessors(MlirOperationState *state, unsigned n, +void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock *successors); -void mlirOperationStateAddAttributes(MlirOperationState *state, unsigned n, +void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute *attributes); /*============================================================================*/ @@ -164,38 +166,38 @@ int mlirOperationIsNull(MlirOperation op); /** Returns the number of regions attached to the given operation. */ -unsigned mlirOperationGetNumRegions(MlirOperation op); +intptr_t mlirOperationGetNumRegions(MlirOperation op); /** Returns `pos`-th region attached to the operation. */ -MlirRegion mlirOperationGetRegion(MlirOperation op, unsigned pos); +MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos); /** Returns an operation immediately following the given operation it its * enclosing block. */ MlirOperation mlirOperationGetNextInBlock(MlirOperation op); /** Returns the number of operands of the operation. */ -unsigned mlirOperationGetNumOperands(MlirOperation op); +intptr_t mlirOperationGetNumOperands(MlirOperation op); /** Returns `pos`-th operand of the operation. */ -MlirValue mlirOperationGetOperand(MlirOperation op, unsigned pos); +MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos); /** Returns the number of results of the operation. */ -unsigned mlirOperationGetNumResults(MlirOperation op); +intptr_t mlirOperationGetNumResults(MlirOperation op); /** Returns `pos`-th result of the operation. */ -MlirValue mlirOperationGetResult(MlirOperation op, unsigned pos); +MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos); /** Returns the number of successor blocks of the operation. */ -unsigned mlirOperationGetNumSuccessors(MlirOperation op); +intptr_t mlirOperationGetNumSuccessors(MlirOperation op); /** Returns `pos`-th successor of the operation. */ -MlirBlock mlirOperationGetSuccessor(MlirOperation op, unsigned pos); +MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos); /** Returns the number of attributes attached to the operation. */ -unsigned mlirOperationGetNumAttributes(MlirOperation op); +intptr_t mlirOperationGetNumAttributes(MlirOperation op); /** Return `pos`-th attribute of the operation. */ -MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, unsigned pos); +MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos); /** Returns an attrbute attached to the operation given its name. */ MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, @@ -223,7 +225,7 @@ /** Takes a block owned by the caller and inserts it at `pos` to the given * region. */ -void mlirRegionInsertOwnedBlock(MlirRegion region, unsigned pos, +void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block); /*============================================================================*/ @@ -232,7 +234,7 @@ /** Creates a new empty block with the given argument types and transfers * ownership to the caller. */ -MlirBlock mlirBlockCreate(unsigned nArgs, MlirType *args); +MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType *args); /** Takes a block owned by the caller and destroys it. */ void mlirBlockDestroy(MlirBlock block); @@ -252,14 +254,14 @@ /** Takes an operation owned by the caller and inserts it as `pos` to the block. */ -void mlirBlockInsertOwnedOperation(MlirBlock block, unsigned pos, +void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, MlirOperation operation); /** Returns the number of arguments of the block. */ -unsigned mlirBlockGetNumArguments(MlirBlock block); +intptr_t mlirBlockGetNumArguments(MlirBlock block); /** Returns `pos`-th argument of the block. */ -MlirValue mlirBlockGetArgument(MlirBlock block, unsigned pos); +MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos); /*============================================================================*/ /* Value API. */ diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -40,7 +40,7 @@ DEFINE_C_API_METHODS(MlirModule, ModuleOp) template -static ArrayRef unwrapList(unsigned size, CTy *first, +static ArrayRef unwrapList(intptr_t size, CTy *first, SmallVectorImpl &storage) { static_assert( std::is_same())), CppTy>::value, @@ -51,7 +51,7 @@ assert(storage.empty() && "expected to populate storage"); storage.reserve(size); - for (unsigned i = 0; i < size; ++i) + for (intptr_t i = 0; i < size; ++i) storage.push_back(unwrap(*(first + i))); return storage; } @@ -130,24 +130,24 @@ memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \ state->sizeName += n; -void mlirOperationStateAddResults(MlirOperationState *state, unsigned n, +void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, MlirType *results) { APPEND_ELEMS(MlirType, nResults, results); } -void mlirOperationStateAddOperands(MlirOperationState *state, unsigned n, +void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue *operands) { APPEND_ELEMS(MlirValue, nOperands, operands); } -void mlirOperationStateAddOwnedRegions(MlirOperationState *state, unsigned n, +void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion *regions) { APPEND_ELEMS(MlirRegion, nRegions, regions); } -void mlirOperationStateAddSuccessors(MlirOperationState *state, unsigned n, +void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock *successors) { APPEND_ELEMS(MlirBlock, nSuccessors, successors); } -void mlirOperationStateAddAttributes(MlirOperationState *state, unsigned n, +void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute *attributes) { APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes); } @@ -169,67 +169,67 @@ unwrapList(state->nSuccessors, state->successors, successorStorage)); cppState.attributes.reserve(state->nAttributes); - for (unsigned i = 0; i < state->nAttributes; ++i) + for (intptr_t i = 0; i < state->nAttributes; ++i) cppState.addAttribute(state->attributes[i].name, unwrap(state->attributes[i].attribute)); - for (unsigned i = 0; i < state->nRegions; ++i) + for (intptr_t i = 0; i < state->nRegions; ++i) cppState.addRegion(std::unique_ptr(unwrap(state->regions[i]))); + MlirOperation result = wrap(Operation::create(cppState)); free(state->results); free(state->operands); - free(state->regions); free(state->successors); + free(state->regions); free(state->attributes); - - return wrap(Operation::create(cppState)); + return result; } void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); } int mlirOperationIsNull(MlirOperation op) { return unwrap(op) == nullptr; } -unsigned mlirOperationGetNumRegions(MlirOperation op) { - return unwrap(op)->getNumRegions(); +intptr_t mlirOperationGetNumRegions(MlirOperation op) { + return static_cast(unwrap(op)->getNumRegions()); } -MlirRegion mlirOperationGetRegion(MlirOperation op, unsigned pos) { - return wrap(&unwrap(op)->getRegion(pos)); +MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) { + return wrap(&unwrap(op)->getRegion(static_cast(pos))); } MlirOperation mlirOperationGetNextInBlock(MlirOperation op) { return wrap(unwrap(op)->getNextNode()); } -unsigned mlirOperationGetNumOperands(MlirOperation op) { - return unwrap(op)->getNumOperands(); +intptr_t mlirOperationGetNumOperands(MlirOperation op) { + return static_cast(unwrap(op)->getNumOperands()); } -MlirValue mlirOperationGetOperand(MlirOperation op, unsigned pos) { - return wrap(unwrap(op)->getOperand(pos)); +MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) { + return wrap(unwrap(op)->getOperand(static_cast(pos))); } -unsigned mlirOperationGetNumResults(MlirOperation op) { - return unwrap(op)->getNumResults(); +intptr_t mlirOperationGetNumResults(MlirOperation op) { + return static_cast(unwrap(op)->getNumResults()); } -MlirValue mlirOperationGetResult(MlirOperation op, unsigned pos) { - return wrap(unwrap(op)->getResult(pos)); +MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) { + return wrap(unwrap(op)->getResult(static_cast(pos))); } -unsigned mlirOperationGetNumSuccessors(MlirOperation op) { - return unwrap(op)->getNumSuccessors(); +intptr_t mlirOperationGetNumSuccessors(MlirOperation op) { + return static_cast(unwrap(op)->getNumSuccessors()); } -MlirBlock mlirOperationGetSuccessor(MlirOperation op, unsigned pos) { - return wrap(unwrap(op)->getSuccessor(pos)); +MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) { + return wrap(unwrap(op)->getSuccessor(static_cast(pos))); } -unsigned mlirOperationGetNumAttributes(MlirOperation op) { - return unwrap(op)->getAttrs().size(); +intptr_t mlirOperationGetNumAttributes(MlirOperation op) { + return static_cast(unwrap(op)->getAttrs().size()); } -MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, unsigned pos) { +MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) { NamedAttribute attr = unwrap(op)->getAttrs()[pos]; return MlirNamedAttribute{attr.first.c_str(), wrap(attr.second)}; } @@ -258,7 +258,7 @@ unwrap(region)->push_back(unwrap(block)); } -void mlirRegionInsertOwnedBlock(MlirRegion region, unsigned pos, +void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block) { auto &blockList = unwrap(region)->getBlocks(); blockList.insert(std::next(blockList.begin(), pos), unwrap(block)); @@ -274,9 +274,9 @@ /* Block API. */ /* ========================================================================== */ -MlirBlock mlirBlockCreate(unsigned nArgs, MlirType *args) { +MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType *args) { Block *b = new Block; - for (unsigned i = 0; i < nArgs; ++i) + for (intptr_t i = 0; i < nArgs; ++i) b->addArgument(unwrap(args[i])); return wrap(b); } @@ -296,7 +296,7 @@ unwrap(block)->push_back(unwrap(operation)); } -void mlirBlockInsertOwnedOperation(MlirBlock block, unsigned pos, +void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, MlirOperation operation) { auto &opList = unwrap(block)->getOperations(); opList.insert(std::next(opList.begin(), pos), unwrap(operation)); @@ -306,12 +306,12 @@ int mlirBlockIsNull(MlirBlock block) { return unwrap(block) == nullptr; } -unsigned mlirBlockGetNumArguments(MlirBlock block) { - return unwrap(block)->getNumArguments(); +intptr_t mlirBlockGetNumArguments(MlirBlock block) { + return static_cast(unwrap(block)->getNumArguments()); } -MlirValue mlirBlockGetArgument(MlirBlock block, unsigned pos) { - return wrap(unwrap(block)->getArgument(pos)); +MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) { + return wrap(unwrap(block)->getArgument(static_cast(pos))); } /* ========================================================================== */