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

Commit

Permalink
Update the SPV dialect type parser to use the methods on DialectAsmPa…
Browse files Browse the repository at this point in the history
…rser directly.

This simplifies the implementation quite a bit, and removes the need for explicit string munging. One change is made to some of the enum elements of SPV_DimAttr to ensure that they are proper identifiers; The string form is now prefixed with 'Dim'.

PiperOrigin-RevId: 278027132
  • Loading branch information
River707 authored and tensorflower-gardener committed Nov 1, 2019
1 parent 6fbcb80 commit a4b11eb
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 315 deletions.
6 changes: 3 additions & 3 deletions include/mlir/Dialect/SPIRV/SPIRVBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@ def SPV_DecorationAttr :
let cppNamespace = "::mlir::spirv";
}

def SPV_D_1D : I32EnumAttrCase<"1D", 0>;
def SPV_D_2D : I32EnumAttrCase<"2D", 1>;
def SPV_D_3D : I32EnumAttrCase<"3D", 2>;
def SPV_D_1D : I32EnumAttrCase<"Dim1D", 0>;
def SPV_D_2D : I32EnumAttrCase<"Dim2D", 1>;
def SPV_D_3D : I32EnumAttrCase<"Dim3D", 2>;
def SPV_D_Cube : I32EnumAttrCase<"Cube", 3>;
def SPV_D_Rect : I32EnumAttrCase<"Rect", 4>;
def SPV_D_Buffer : I32EnumAttrCase<"Buffer", 5>;
Expand Down
23 changes: 19 additions & 4 deletions include/mlir/IR/DialectImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,26 @@ class DialectAsmParser {
virtual ParseResult parseFloat(double &result) = 0;

/// Parse an integer value from the stream.
virtual ParseResult parseInteger(uint64_t &result) = 0;

template <typename IntT> ParseResult parseInteger(IntT &result) {
auto loc = getCurrentLocation();
OptionalParseResult parseResult = parseOptionalInteger(result);
if (!parseResult.hasValue())
return emitError(loc, "expected integer value");
return *parseResult;
}

/// Parse an optional integer value from the stream.
virtual OptionalParseResult parseOptionalInteger(uint64_t &result) = 0;

template <typename IntT>
OptionalParseResult parseOptionalInteger(IntT &result) {
auto loc = getCurrentLocation();

// Parse the unsigned variant.
uint64_t uintResult;
if (failed(parseInteger(uintResult)))
return failure();
OptionalParseResult parseResult = parseOptionalInteger(uintResult);
if (!parseResult.hasValue() || failed(*parseResult))
return parseResult;

// Try to convert to the provided integer type.
result = IntT(uintResult);
Expand Down Expand Up @@ -222,6 +234,9 @@ class DialectAsmParser {
/// Parse a '>' token.
virtual ParseResult parseGreater() = 0;

/// Parse a `>` token if present.
virtual ParseResult parseOptionalGreater() = 0;

/// Parse a `(` token.
virtual ParseResult parseLParen() = 0;

Expand Down
24 changes: 24 additions & 0 deletions include/mlir/IR/OpDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ class ParseResult : public LogicalResult {
/// Failure is true in a boolean context.
explicit operator bool() const { return failed(*this); }
};
/// This class implements `Optional` functionality for ParseResult. We don't
/// directly use llvm::Optional here, because it provides an implicit conversion
/// to 'bool' which we want to avoid. This class is used to implement tri-state
/// 'parseOptional' functions that may have a failure mode when parsing that
/// shouldn't be attributed to "not present".
class OptionalParseResult {
public:
OptionalParseResult() = default;
OptionalParseResult(LogicalResult result) : impl(result) {}
OptionalParseResult(ParseResult result) : impl(result) {}
OptionalParseResult(const InFlightDiagnostic &)
: OptionalParseResult(failure()) {}
OptionalParseResult(llvm::NoneType) : impl(llvm::None) {}

/// Returns true if we contain a valid ParseResult value.
bool hasValue() const { return impl.hasValue(); }

/// Access the internal ParseResult value.
ParseResult getValue() const { return impl.getValue(); }
ParseResult operator*() const { return getValue(); }

private:
Optional<ParseResult> impl;
};

// These functions are out-of-line utilities, which avoids them being template
// instantiated/duplicated.
Expand Down
Loading

0 comments on commit a4b11eb

Please sign in to comment.