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

Conversation

@denis0x0D
Copy link
Contributor

This patch adds other binary arithmetic operations such as: OpUdiv, OpSDiv, OpUMod, OpSRem, OpSMod.

@antiagainst can you please take a look?

BTW, small question about other spirv operations such as logical operations.
In case of spirv all logical binary operations return scalar boolean or vector of boolean, would it be valid to split binary operation to arithmetic and logical like this:

class SPV_BinaryOp<string mnemonic, Type resultType, Type operandsType,
                   list<OpTrait> traits = []> :
      SPV_Op<mnemonic, traits> {
  let arguments = (ins
    SPV_ScalarOrVectorOf<operandsType>:$operand1,
    SPV_ScalarOrVectorOf<operandsType>:$operand2
  );
  let results = (outs
    SPV_ScalarOrVectorOf<resultType>:$result
  );
  let parser = [{ return impl::parseBinaryOp(parser, result); }];
  let printer = [{ return impl::printBinaryOp(getOperation(), p); }];
  // No additional verification needed in addition to the ODS-generated ones.
  let verifier = [{ return success(); }];
}

class SPV_ArithmeticOp<string mnemonic, Type type,
                       list<OpTrait> traits = []> :
      // Operands type same as result type.
      SPV_BinaryOp<mnemonic, type, type,
                   !listconcat(traits,
                               [NoSideEffect, SameOperandsAndResultType])> {
}

class SPV_LogicalOp<string mnemonic, Type operandsType,
                    list<OpTrait> traits = []> :
      // Result type is SPV_Bool.
      SPV_BinaryOp<mnemonic, SPV_Bool, operandsType,
                   !listconcat(traits, [NoSideEffect, SameTypeOperands])> {
}

Add binary operations such as: OpUdiv, OpSDiv, OpUMod, OpSRem, OpSMod.
Copy link
Contributor

@antiagainst antiagainst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@antiagainst
Copy link
Contributor

@denis0x0D: The split sounds reasonable to me!

@MaheshRavishankar
Copy link
Contributor

@denis0x0D : Splitting it sounds like a good idea.

@denis0x0D
Copy link
Contributor Author

@antiagainst @MaheshRavishankar thanks a lot for review!
BTW, did anyone face the serialization failing tests (make check-mlir) , seems like after this commit 8ffc2d9 , but I might miss something, please fix me if I'm wrong.

*******************
Testing Time: 2.33s
********************
Failing Tests (2):
    MLIR :: Dialect/SPIRV/Serialization/entry_interface.mlir
    MLIR :: Dialect/SPIRV/Serialization/variables.mlir

  Expected Passes    : 229
  Unsupported Tests  : 4
  Unexpected Failures: 2
make[3]: *** [projects/mlir/test/CMakeFiles/check-mlir.dir/build.make:58: projects/mlir/test/CMakeFiles/check-mlir] Error 1
make[2]: *** [CMakeFiles/Makefile2:17467: projects/mlir/test/CMakeFiles/check-mlir.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:17474: projects/mlir/test/CMakeFiles/check-mlir.dir/rule] Error 2
make: *** [Makefile:5282: check-mlir] Error 2

@MaheshRavishankar
Copy link
Contributor

No, I am not seeing that failure. Let me try building this again and check it.

@MaheshRavishankar
Copy link
Contributor

I am not seeing this failure. @antiagainst : do you see this failure?

@denis0x0D : Just to confirm, you are syncing to ToT?

@antiagainst
Copy link
Contributor

I wasn't able to reproduce the error either (ninja 1.8.2, GCC 7.4.0). What's your environment?

@denis0x0D
Copy link
Contributor Author

@MaheshRavishankar thanks for the answer, I'm on the master branch now, here is my backtrace:

#14 0x0000561f1a5eedfd mlir::LogicalResult (anonymous namespace)::Serializer::processOp<mlir::spirv::VariableOp>(mlir::spirv::VariableOp) /home/llvm-project/build/projects/mlir/include/mlir/Dialect/SPIRV/SPIRVSerialization.inc:646:40
#15 0x0000561f1a5ef4ae (anonymous namespace)::Serializer::dispatchToAutogenSerialization(mlir::Operation*) /home/llvm-project/build/projects/mlir/include/mlir/Dialect/SPIRV/SPIRVSerialization.inc:699:68
#16 0x0000561f1a5e973e (anonymous namespace)::Serializer::processOperation(mlir::Operation*) /home/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp:872:1
#17 0x0000561f1a5e5c3f (anonymous namespace)::Serializer::serialize() /home/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp:278:15
#18 0x0000561f1a5ef67a mlir::spirv::serialize(mlir::spirv::ModuleOp, llvm::SmallVectorImpl<unsigned int>&) /home/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp:945:13
#19 0x0000561f1a5cde49 serializeModule(mlir::ModuleOp, llvm::StringRef)::'lambda'(mlir::spirv::ModuleOp)::operator()(mlir::spirv::ModuleOp) const /home/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/ConvertToBinary.cpp:57:52

it migth be something wrong with my env, but the strange thing is that all tests passes right before this commit. Thanks.

@MaheshRavishankar
Copy link
Contributor

thanks for the trace. So this segfaulting? Since the segfault seems to be happening in the autogenerated file, do you mind posting some of the code around the segfaulting line number?

@denis0x0D
Copy link
Contributor Author

@antiagainst @MaheshRavishankar I'm building with Unix like MakeFile, not ninja, so, might be this is a problem.

cmake ../llvm -DLLVM_BUILD_EXAMPLES=ON -DLLVM_ENABLE_CXX1Y=Y -DLLVM_TARGETS_TO_BUILD="host"

make check-mlir -j

sorry for bothering you.

@denis0x0D
Copy link
Contributor Author

@MaheshRavishankar it points to the line #129 on the fail
8ffc2d9#diff-fd95d2394fb0b7917cd33288de27b39fR129

@MaheshRavishankar
Copy link
Contributor

Not a bother at all. If there is an issue, would rather fix it, so this is good to know.

@MaheshRavishankar
Copy link
Contributor

Dont think it is an issue with Ninja. What is your compiler?

tensorflow-copybara pushed a commit to tensorflow/tensorflow that referenced this pull request Jul 31, 2019
Add binary operations such as: OpUdiv, OpSDiv, OpUMod, OpSRem, OpSMod.

Closes #56

COPYBARA_INTEGRATE_REVIEW=tensorflow/mlir#56 from denis0x0D:sandbox/bin_ops_int 4959325a693b4658b978a8b97f79b8237eb39764
PiperOrigin-RevId: 260961681
@denis0x0D
Copy link
Contributor Author

@MaheshRavishankar it fails on ninja too, I'm building with gcc 8.2.0
Here is a full backtrace:

FAIL: MLIR :: Dialect/SPIRV/Serialization/variables.mlir (245 of 247)
******************** TEST 'MLIR :: Dialect/SPIRV/Serialization/variables.mlir' FAILED ********************
Script:
--
: 'RUN: at line 1';   /home/khalikov/llvm-project/build/bin/mlir-translate -serialize-spirv /home/khalikov/llvm-project/llvm/projects/mlir/test/Dialect/SPIRV/Serialization/variables.mlir | /home/khalikov/llvm-project/build/bin/mlir-translate -deserialize-spirv | /home/khalikov/llvm-project/build/bin/FileCheck /home/khalikov/llvm-project/llvm/projects/mlir/test/Dialect/SPIRV/Serialization/variables.mlir
--
Exit Code: 2

Command Output (stderr):
--
mlir-translate: /home/khalikov/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:417: T& llvm::MutableArrayRef<T>::operator[](size_t) const [with T = mlir::IROperandImpl<mlir::Value>; size_t = long unsigned int]: Assertion `Index < this->size() && "Invalid index!"' failed.
Stack dump:
0.	Program arguments: /home/khalikov/llvm-project/build/bin/mlir-translate -serialize-spirv /home/khalikov/llvm-project/llvm/projects/mlir/test/Dialect/SPIRV/Serialization/variables.mlir 
1.	Program arguments: /home/khalikov/llvm-project/build/bin/mlir-translate -serialize-spirv /home/khalikov/llvm-project/llvm/projects/mlir/test/Dialect/SPIRV/Serialization/variables.mlir 
 #0 0x0000556be9dde1f7 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/khalikov/llvm-project/llvm/lib/Support/Unix/Signals.inc:494:22
 #1 0x0000556be9dde28a PrintStackTraceSignalHandler(void*) /home/khalikov/llvm-project/llvm/lib/Support/Unix/Signals.inc:555:1
 #2 0x0000556be9ddc278 llvm::sys::RunSignalHandlers() /home/khalikov/llvm-project/llvm/lib/Support/Signals.cpp:68:20
 #3 0x0000556be9dddc4d SignalHandler(int) /home/khalikov/llvm-project/llvm/lib/Support/Unix/Signals.inc:357:1
 #4 0x00007fd90a29edd0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12dd0)
 #5 0x00007fd909db0077 raise /build/glibc-B9XfQf/glibc-2.28/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #6 0x00007fd909d91535 abort /build/glibc-B9XfQf/glibc-2.28/stdlib/abort.c:81:7
 #7 0x00007fd909d9140f get_sysdep_segment_value /build/glibc-B9XfQf/glibc-2.28/intl/loadmsgcat.c:509:8
 #8 0x00007fd909d9140f _nl_load_domain /build/glibc-B9XfQf/glibc-2.28/intl/loadmsgcat.c:970:34
 #9 0x00007fd909da1142 (/lib/x86_64-linux-gnu/libc.so.6+0x32142)
#10 0x0000556be9a12a73 llvm::MutableArrayRef<mlir::IROperandImpl<mlir::Value> >::operator[](unsigned long) const /home/khalikov/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:417:7
#11 0x0000556be9a0e0cb mlir::Operation::getOpOperand(unsigned int) /home/khalikov/llvm-project/llvm/projects/mlir/include/mlir/IR/Operation.h:209:72
#12 0x0000556be9a0e020 mlir::Operation::getOperand(unsigned int) /home/khalikov/llvm-project/llvm/projects/mlir/include/mlir/IR/Operation.h:187:65
#13 0x0000556be9a0e4c3 mlir::OperandIterator::operator*() const /home/khalikov/llvm-project/llvm/projects/mlir/include/mlir/IR/Operation.h:586:76
#14 0x0000556be9b9adc9 mlir::LogicalResult (anonymous namespace)::Serializer::processOp<mlir::spirv::VariableOp>(mlir::spirv::VariableOp) /home/khalikov/llvm-project/build/projects/mlir/include/mlir/Dialect/SPIRV/SPIRVSerialization.inc:646:40
#15 0x0000556be9b9b47a (anonymous namespace)::Serializer::dispatchToAutogenSerialization(mlir::Operation*) /home/khalikov/llvm-project/build/projects/mlir/include/mlir/Dialect/SPIRV/SPIRVSerialization.inc:699:68
#16 0x0000556be9b9570a (anonymous namespace)::Serializer::processOperation(mlir::Operation*) /home/khalikov/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp:872:1
#17 0x0000556be9b91c0b (anonymous namespace)::Serializer::serialize() /home/khalikov/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp:278:15
#18 0x0000556be9b9b646 mlir::spirv::serialize(mlir::spirv::ModuleOp, llvm::SmallVectorImpl<unsigned int>&) /home/khalikov/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp:945:13
#19 0x0000556be9b79e15 serializeModule(mlir::ModuleOp, llvm::StringRef)::'lambda'(mlir::spirv::ModuleOp)::operator()(mlir::spirv::ModuleOp) const /home/khalikov/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/ConvertToBinary.cpp:57:52
#20 0x0000556be9b7a1c3 void llvm::function_ref<void (mlir::spirv::ModuleOp)>::callback_fn<serializeModule(mlir::ModuleOp, llvm::StringRef)::'lambda'(mlir::spirv::ModuleOp)>(long, mlir::spirv::ModuleOp) /home/khalikov/llvm-project/llvm/include/llvm/ADT/STLExtras.h:126:40
#21 0x0000556be9b7a75f llvm::function_ref<void (mlir::spirv::ModuleOp)>::operator()(mlir::spirv::ModuleOp) const /home/khalikov/llvm-project/llvm/include/llvm/ADT/STLExtras.h:142:62
#22 0x0000556be9b7a5dd void mlir::OpState::walk<mlir::spirv::ModuleOp>(llvm::function_ref<void (mlir::spirv::ModuleOp)>)::'lambda'(mlir::Operation*)::operator()(mlir::Operation*) const /home/khalikov/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:198:5
#23 0x0000556be9b7a87f void llvm::function_ref<void (mlir::Operation*)>::callback_fn<void mlir::OpState::walk<mlir::spirv::ModuleOp>(llvm::function_ref<void (mlir::spirv::ModuleOp)>)::'lambda'(mlir::Operation*)>(long, mlir::Operation*) /home/khalikov/llvm-project/llvm/include/llvm/ADT/STLExtras.h:126:40
#24 0x0000556be9d1b15b llvm::function_ref<void (mlir::Operation*)>::operator()(mlir::Operation*) const /home/khalikov/llvm-project/llvm/include/llvm/ADT/STLExtras.h:142:62
#25 0x0000556be9d15b95 mlir::Operation::walk(llvm::function_ref<void (mlir::Operation*)>) /home/khalikov/llvm-project/llvm/projects/mlir/lib/IR/Operation.cpp:304:1
#26 0x0000556be9cd7641 mlir::Block::walk(llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Operation, true, false, void>, false, false>, llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Operation, true, false, void>, false, false>, llvm::function_ref<void (mlir::Operation*)>) /home/khalikov/llvm-project/llvm/projects/mlir/lib/IR/Block.cpp:239:74
#27 0x0000556be9cd755a mlir::Block::walk(llvm::function_ref<void (mlir::Operation*)>) /home/khalikov/llvm-project/llvm/projects/mlir/lib/IR/Block.cpp:233:1
#28 0x0000556be9d228e0 mlir::Region::walk(llvm::function_ref<void (mlir::Operation*)>) /home/khalikov/llvm-project/llvm/projects/mlir/lib/IR/Region.cpp:174:23
#29 0x0000556be9d15b7b mlir::Operation::walk(llvm::function_ref<void (mlir::Operation*)>) /home/khalikov/llvm-project/llvm/projects/mlir/lib/IR/Operation.cpp:299:3
#30 0x0000556be9a8a2d3 mlir::OpState::walk(llvm::function_ref<void (mlir::Operation*)>) /home/khalikov/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:194:3
#31 0x0000556be9b7a657 void mlir::OpState::walk<mlir::spirv::ModuleOp>(llvm::function_ref<void (mlir::spirv::ModuleOp)>) /home/khalikov/llvm-project/llvm/projects/mlir/include/mlir/IR/OpDefinition.h:202:3
#32 0x0000556be9b79fa9 serializeModule(mlir::ModuleOp, llvm::StringRef) /home/khalikov/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/ConvertToBinary.cpp:49:40
#33 0x0000556be9b7a0f7 'lambda'(mlir::ModuleOp, llvm::StringRef)::operator()(mlir::ModuleOp, llvm::StringRef) const /home/khalikov/llvm-project/llvm/projects/mlir/lib/Dialect/SPIRV/Serialization/ConvertToBinary.cpp:79:18
#34 0x0000556be9b7a28a _ZNSt17_Function_handlerIFN4mlir13LogicalResultENS0_8ModuleOpEN4llvm9StringRefEEUlS2_S4_E_E9_M_invokeERKSt9_Any_dataOS2_OS4_ /usr/include/c++/8/bits/std_function.h:284:7
#35 0x0000556be9d338ab std::function<mlir::LogicalResult (mlir::ModuleOp, llvm::StringRef)>::operator()(mlir::ModuleOp, llvm::StringRef) const /usr/include/c++/8/bits/std_function.h:688:5
#36 0x0000556be9d32540 mlir::TranslationParser::TranslationParser(llvm::cl::Option&)::'lambda0'(llvm::StringRef, llvm::StringRef, mlir::MLIRContext*)::operator()(llvm::StringRef, llvm::StringRef, mlir::MLIRContext*) const /home/khalikov/llvm-project/llvm/projects/mlir/lib/Support/TranslateClParser.cpp:87:51
#37 0x0000556be9d32dcb std::_Function_handler<mlir::LogicalResult (llvm::StringRef, llvm::StringRef, mlir::MLIRContext*), mlir::TranslationParser::TranslationParser(llvm::cl::Option&)::'lambda0'(llvm::StringRef, llvm::StringRef, mlir::MLIRContext*)>::_M_invoke(std::_Any_data const&, llvm::StringRef&&, llvm::StringRef&&, mlir::MLIRContext*&&) /usr/include/c++/8/bits/std_function.h:284:7
#38 0x0000556be9c91a56 std::function<mlir::LogicalResult (llvm::StringRef, llvm::StringRef, mlir::MLIRContext*)>::operator()(llvm::StringRef, llvm::StringRef, mlir::MLIRContext*) const /usr/include/c++/8/bits/std_function.h:688:5
#39 0x0000556be9c9115e main /home/khalikov/llvm-project/llvm/projects/mlir/tools/mlir-translate/mlir-translate.cpp:50:16
#40 0x00007fd909d9309b __libc_start_main /build/glibc-B9XfQf/glibc-2.28/csu/../csu/libc-start.c:342:3
#41 0x0000556be99fc17a _start (/home/khalikov/llvm-project/build/bin/mlir-translate+0x5417a)
error: SPIR-V binary module must have a 5-word header
FileCheck error: '-' is empty.
FileCheck command line:  /home/khalikov/llvm-project/build/bin/FileCheck /home/khalikov/llvm-project/llvm/projects/mlir/test/Dialect/SPIRV/Serialization/variables.mlir

the stack-frame #14 points
to this function


26 Serializer::processOp<spirv::VariableOp>(
 627   spirv::VariableOp op) {
 628   SmallVector<uint32_t, 4> operands;
 629   SmallVector<StringRef, 2> elidedAttrs;
 630   uint32_t resultTypeID = 0;
 631   if (failed(processType(op.getLoc(), op.getType(), resultTypeID))) {
 632     return failure();
 633   }
 634   operands.push_back(resultTypeID);
 635   auto resultID = getNextID();
 636   valueIDMap[op.getResult()] = resultID;
 637   operands.push_back(resultID);
 638   {
 639     auto attr = op.getAttr("storage_class");
 640     if (attr) {
 641       operands.push_back(static_cast<uint32_t>(attr.cast<IntegerAttr>().getValue().getZExtValue(     )));
 642     }
 643     elidedAttrs.push_back("storage_class");
 644   }
 645   {
 646     for (auto arg : op.getODSOperands(1)) {
 647       auto argID = findValueID(arg);
 648       if (!argID) {
 649         emitError(op.getLoc(), "operand 0 has a use before def");
 650       }
 651       operands.push_back(argID);
 652     }
 653   }
 654   encodeInstructionInto(functions, spirv::getOpcode<spirv::VariableOp>(), operands);
 655   for (auto attr : op.getAttrs()) 



but if noone face it and CI tests passed, this is something wrong with my env.
Sorry again.

@MaheshRavishankar
Copy link
Contributor

I think there might be an issue. the getODSOperands(1) looks suspect to me. I'll take a look.

@MaheshRavishankar
Copy link
Contributor

That's definitely the issue. Have a fix in flight. Thanks for bringing it to our attention. We use gcc 7.4.2. and that doesn't seem to result in a segfault.

@denis0x0D
Copy link
Contributor Author

@MaheshRavishankar thanks for looking on this!

@antiagainst
Copy link
Contributor

Thanks @MaheshRavishankar for 32b81a8, which should have it fixed. @denis0x0D please try it out.

@denis0x0D
Copy link
Contributor Author

@antiagainst @MaheshRavishankar works for me, thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants