[mlir] use intptr_t in C API

Using intptr_t is a consensus for MLIR C API, but the change was missing
from 75f239e975 (that was using unsigned initially) due to a
misrebase.

Reviewed By: stellaraccident, mehdi_amini

Differential Revision: https://reviews.llvm.org/D85751
This commit is contained in:
Alex Zinenko
2020-08-11 18:34:32 +02:00
parent e441b7a7a0
commit af838584ec
3 changed files with 71 additions and 66 deletions

View File

@@ -89,8 +89,11 @@ regions, results and successors.
For indexed components, the following pair of functions is provided.
- `unsigned mlirXGetNum<Y>s(MlirX)` returns the upper bound on the index.
- `MlirY mlirXGet<Y>(MlirX, unsigned pos)` returns 'pos'-th subobject.
- `intptr_t mlirXGetNum<Y>s(MlirX)` returns the upper bound on the index.
- `MlirY mlirXGet<Y>(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

View File

@@ -18,6 +18,8 @@
#ifndef MLIR_C_IR_H
#define MLIR_C_IR_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -122,15 +124,15 @@ MlirOperation mlirModuleGetOperation(MlirModule module);
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 @@ typedef struct MlirOperationState MlirOperationState;
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 @@ void mlirOperationDestroy(MlirOperation op);
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 @@ void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block);
/** 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 @@ void mlirRegionInsertOwnedBlock(MlirRegion region, unsigned pos,
/** 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 @@ void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation);
/** 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. */

View File

@@ -40,7 +40,7 @@ DEFINE_C_API_METHODS(MlirValue, Value)
DEFINE_C_API_METHODS(MlirModule, ModuleOp)
template <typename CppTy, typename CTy>
static ArrayRef<CppTy> unwrapList(unsigned size, CTy *first,
static ArrayRef<CppTy> unwrapList(intptr_t size, CTy *first,
SmallVectorImpl<CppTy> &storage) {
static_assert(
std::is_same<decltype(unwrap(std::declval<CTy>())), CppTy>::value,
@@ -51,7 +51,7 @@ static ArrayRef<CppTy> unwrapList(unsigned size, CTy *first,
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 @@ MlirOperationState mlirOperationStateGet(const char *name, MlirLocation loc) {
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 @@ MlirOperation mlirOperationCreate(const MlirOperationState *state) {
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<Region>(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<intptr_t>(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<unsigned>(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<intptr_t>(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<unsigned>(pos)));
}
unsigned mlirOperationGetNumResults(MlirOperation op) {
return unwrap(op)->getNumResults();
intptr_t mlirOperationGetNumResults(MlirOperation op) {
return static_cast<intptr_t>(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<unsigned>(pos)));
}
unsigned mlirOperationGetNumSuccessors(MlirOperation op) {
return unwrap(op)->getNumSuccessors();
intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {
return static_cast<intptr_t>(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<unsigned>(pos)));
}
unsigned mlirOperationGetNumAttributes(MlirOperation op) {
return unwrap(op)->getAttrs().size();
intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
return static_cast<intptr_t>(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 @@ void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
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 @@ int mlirRegionIsNull(MlirRegion region) { return unwrap(region) == nullptr; }
/* 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 @@ void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
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 @@ void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
int mlirBlockIsNull(MlirBlock block) { return unwrap(block) == nullptr; }
unsigned mlirBlockGetNumArguments(MlirBlock block) {
return unwrap(block)->getNumArguments();
intptr_t mlirBlockGetNumArguments(MlirBlock block) {
return static_cast<intptr_t>(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<unsigned>(pos)));
}
/* ========================================================================== */