Is it possible to specify memory alignment?

Hello,

I’ve been looking in the documentation, but was not able to find how to specify the alignment of data. Things like:

  • the alignment of a memref
  • the alignment of an alloc() operation
  • the alignment of a global variable

Is there a way to express them ?

Best,
Dumitru

Can I know where you were looking for it in the docs? The docs do have a lot of information on memref alignment:
https://mlir.llvm.org/docs/Dialects/Standard/ (search for ‘alignment’)

For one, in the language reference, where the definition of memrefs includes nothing on alignment. From your link (thanks for it!) it seems that alignment is not part of the type.

Since we’re at it, I’ve also tried several times (without success, as yet) to understand how to define global variables. I’ve tried the syntax (provided here):

llvm.mlir.global @variable(32.0 : f32) : !llvm.float

But for some reason mlir-opt returns an error which I don’t understand yet:

custom op ‘llvm.mlir.global’ expected linkage

I also don’t see how I could specify a fixed address for a global variable.

BTW: if you wonder why I’m doing this - I’m still not yet happy with my understanding of tiling, and I want to understand the source of some speed variations. One source I want to investigate is indeed the alignment of the various matrices.

In general if you hit such an issue, a good way to figure it out is to start from the tests. In this case it seems like the doc examples weren’t updated after this change (@ftynse)

1 Like

What is the equivalent LLVM IR?

Thanks for investigating! I think we should actually allow omitting the linkage, as we already do for functions, - ⚙ D78096 [mlir] LLVM dialect: support globals without linkage keyword, assuming 'external' for the fix.

This is a very good question. I’m experienced with low-level C code generation, but I have no experience with LLVMIR.

In low-level C, at least part of the information is provided as part of a linker script. You need to do this in order (for instance) to guarantee performance by guaranteeing relative alignment between variables, and thus consistent cache behavior.

The attributes of gcc/llvm allow the encoding of alignment, but:

  1. I don’t know how LLVM encodes this (I have no experience with LLVMIR).
  2. There are things LLVM does not cover, as they are covered by linking. I was wondering if MLIR is meant to cover them (such as allocating a variable to a precise address).

Related to the last point: Is mlir-cpu-runner able to take into account linker-script-level information?

If you have C input, you can see the LLVM IR produced by clang by running clang input.c -S -emit-llvm and inspecting input.ll.

MLIR may have a dialect for virtually anything, but I’m unaware of any linking-related dialect atm. I expect linking to be mostly symbol name-based, so if you get MLIR to produce LLVM with the symbol names your linker expects, chances are you can use the same linker flags.