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

Conversation

@whchung
Copy link

@whchung whchung commented Aug 2, 2019

Add supporting logic to lower MLIR module into ROCm-Device-Libs (ROCDL) IR.

Follow-up to #57.


#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"
namespace mlir {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a newline after the includes.

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.


class ROCDLDialect : public Dialect {
public:
explicit ROCDLDialect(MLIRContext *context);
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll likely want to provide a static 'getDialectNamespace' utility. This is used by several template mechanisms:
static StringRef getDialectNamespace() { return "..."; }

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.


#include "llvm/ADT/StringSwitch.h"

namespace mlir {
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop the outer 'mlir' namespace.

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.


} // anonymous namespace

FunctionPassBase *createLowerGpuOpsToROCDLOpsPass() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should be qualified with mlir:: and not within a namespace.

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.

#include "llvm/Support/SourceMgr.h"

namespace mlir {
namespace ROCDL {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't place these within namespaces, prefer explicit qualification + using directives.

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.

using namespace mlir;

namespace {
static llvm::Value *createIntrinsicCall(llvm::IRBuilder<> &builder,
Copy link
Contributor

Choose a reason for hiding this comment

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

static methods should be in the top-level namespace.

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.

// RUN: mlir-opt %s | FileCheck %s

func @rocdl_special_regs() -> !llvm.i32 {
// CHECK: %0 = rocdl.workitem.id.x : !llvm.i32
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

@River707 I was following examples depicted at:
https://github.com/tensorflow/mlir/blob/master/test/LLVMIR/nvvm.mlir

How do you recommend to revise the unit test?

Copy link
Contributor

Choose a reason for hiding this comment

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

For here, it should be enough to just drop the SSA value names, e.g. "%0", from the CHECK lines. The CHECK should as constrained as possible and only focus on testing what is important. The SSA value names here are not related to what you are trying to check, i.e. the custom syntax of the ops.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. Will revise.

// RUN: mlir-translate -mlir-to-rocdlir %s | FileCheck %s

func @rocdl_special_regs() -> !llvm.i32 {
// CHECK: %1 = call i32 @llvm.amdgcn.workitem.id.x()
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

Copy link
Author

Choose a reason for hiding this comment

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

Will revise.

@River707
Copy link
Contributor

River707 commented Aug 2, 2019

Make sure to squash all of your changes into one commit.

@whchung
Copy link
Author

whchung commented Aug 2, 2019

@River707 Thanks for your quick review! Pretty much all the issues you pointed out are in NVVM dialect as well. Should I file another PR to change it?

@River707
Copy link
Contributor

River707 commented Aug 2, 2019

Is your question if you should file a PR to cleanup the NVVM dialect? If so that would be awesome, it needs to be cleaned up as well.

// =============================================================================
//
// This file implements a pass to generate ROCDLIR operations for higher-level
// GPU operations.
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like independent from the definition of the dialect, you should be able to split this in a followup PR

Copy link
Author

Choose a reason for hiding this comment

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

ok will split this PR into smaller pieces and file this module as a separate PR.

// =============================================================================
//
// This file implements a translation between the MLIR LLVM + ROCDL dialects and
// LLVM IR with ROCDL intrinsics and metadata.
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like another component that I rather review in a separate followup PR from the introduction of the dialect itself.

Copy link
Author

Choose a reason for hiding this comment

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

ok will split this PR into smaller pieces and file this module as a separate PR.

}

// This function has the "amdgpu_kernel" calling convention after conversion.
// CHECK: amdgpu_kernel
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit (not super important here, but you may write more tests in the future): in general we separate multiple functions testing with CHECK-LABEL: directives, this helps understand failures when the test fail (and makes FileCheck a bit more robust).

// limitations under the License.
// =============================================================================
//
// This file defines the ROCDL IR dialect in MLIR, containing ROCDL operations and
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this line needs to be wrapped.

include "mlir/LLVMIR/LLVMOpBase.td"

def ROCDL_Dialect : Dialect {
let name = "rocdl";
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a description regarding this dialect would be nice.

}

class ROCDL_Op<string mnemonic, list<OpTrait> traits = []> :
LLVM_OpBase<ROCDL_Dialect, mnemonic, traits> {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to have the empty {}.

def ROCDL_BlockIdYOp : ROCDL_SpecialRegisterOp<"workgroup.id.y">;
def ROCDL_BlockIdZOp : ROCDL_SpecialRegisterOp<"workgroup.id.z">;

class ROCDL_DeviceFunctionOp<string mnemonic, string device_function, int parameter,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we format this class a bit nicer (proper line wrapping, etc.)?

// limitations under the License.
// =============================================================================
//
// This file declares the entry point for the MLIR to LLVM + ROCDL IR conversion.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: need wrapping here.

// XOp, YOp and ZOp are assumed to return an `llvm.i32` value. Depending on
// `indexBitwidth`, sign-extend or truncate the resulting value to match the
// bitwidth expected by the consumers of the value.
template <typename XOp, typename YOp, typename ZOp, class Op>
Copy link
Contributor

Choose a reason for hiding this comment

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

This file need to be clang-formated.

newOp = builder.create<ZOp>(loc, LLVM::LLVMType::getInt32Ty(dialect));
break;
default:
operation.emitError("Illegal dimension: " + operation.dimension());
Copy link
Contributor

Choose a reason for hiding this comment

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

The convention is to start error messages with lowercase letters.

parser->parseColonType(type))
return failure();

result->addTypes(type);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can do return parser->addTypeToList(type, state->types); here.

@joker-eph
Copy link
Contributor

Hey @whchung, any update?

@joker-eph
Copy link
Contributor

Closing for now since there hasn't been any activity for a month. Feel free to re-open as needed!

@joker-eph joker-eph closed this Sep 6, 2019
@whchung
Copy link
Author

whchung commented Sep 6, 2019

@joker-eph sorry I was focusing on internal tools based on mlir-rocm-runner lately. I'll revisit the PRs I submitted next week.

@joker-eph
Copy link
Contributor

Good to hear! No hurry, I just closed to keep our open pull-requests count lower as we try to keep an eye on all the active ones.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants