Skip to content

[mlir] do not inject malloc/free in to-LLVM translation #73224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

ftynse
Copy link
Member

@ftynse ftynse commented Nov 23, 2023

In the early days of MLIR-to-LLVM IR translation, it had to forcefully inject declarations of malloc and free functions as then-standard (now memref) dialect ops were unconditionally lowering to libc calls. This is no longer the case. Even when they do lower to libc calls, the signatures of those methods are injected at lowering since calls must target declared functions in valid IR. Don't inject those declarations anymore.

In the early days of MLIR-to-LLVM IR translation, it had to forcefully
inject declarations of `malloc` and `free` functions as then-standard
(now `memref`) dialect ops were unconditionally lowering to libc calls.
This is no longer the case. Even when they do lower to libc calls, the
signatures of those methods are injected at lowering since calls must
target declared functions in valid IR. Don't inject those declarations
anymore.
@llvmbot
Copy link
Member

llvmbot commented Nov 23, 2023

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: Oleksandr "Alex" Zinenko (ftynse)

Changes

In the early days of MLIR-to-LLVM IR translation, it had to forcefully inject declarations of malloc and free functions as then-standard (now memref) dialect ops were unconditionally lowering to libc calls. This is no longer the case. Even when they do lower to libc calls, the signatures of those methods are injected at lowering since calls must target declared functions in valid IR. Don't inject those declarations anymore.


Full diff: https://github.com/llvm/llvm-project/pull/73224.diff

2 Files Affected:

  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (-8)
  • (modified) mlir/test/Target/LLVMIR/llvmir.mlir (+1-1)
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index ef1c8c21d54b08f..c5df0c7ec181a7c 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1320,14 +1320,6 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
           m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
     llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue());
 
-  // Inject declarations for `malloc` and `free` functions that can be used in
-  // memref allocation/deallocation coming from standard ops lowering.
-  llvm::IRBuilder<> builder(llvmContext);
-  llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(),
-                                  builder.getInt64Ty());
-  llvmModule->getOrInsertFunction("free", builder.getVoidTy(),
-                                  builder.getInt8PtrTy());
-
   return llvmModule;
 }
 
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 73fcee0dd2e70bd..ab8506ff163efe6 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -179,7 +179,7 @@ llvm.mlir.global internal constant @sectionvar("teststring")  {section = ".mysec
 // CHECK: declare ptr @malloc(i64)
 llvm.func @malloc(i64) -> !llvm.ptr
 // CHECK: declare void @free(ptr)
-
+llvm.func @free(!llvm.ptr)
 
 //
 // Basic functionality: function and block conversion, function calls,

@ftynse ftynse requested a review from gysit November 23, 2023 12:32
Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

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

LGTM

@ftynse ftynse merged commit 8735b7d into llvm:main Nov 23, 2023
@ftynse ftynse deleted the drop-forceful-injection branch November 23, 2023 12:38
@Hardcode84
Copy link
Contributor

There seems to a failure in main now:

# .---command stderr------------
# | D:\projs\llvm\llvm-project\mlir\test\CAPI\translation.c:49:12: error: CHECK: expected string not found in input
# |  // CHECK: declare ptr @malloc(i64 %{{.*}})
# |            ^
# | <stdin>:1:15: note: scanning from here
# | testToLLVMIR()
# |               ^
# | <stdin>:7:2: note: possible intended match here
# |  ret i64 %3
# |  ^
# |
# | Input file: <stdin>
# | Check file: D:\projs\llvm\llvm-project\mlir\test\CAPI\translation.c
# |
# | -dump-input=help explains the following input dump.
# |
# | Input was:
# | <<<<<<
# |             1: testToLLVMIR()
# | check:49'0                   X error: no match found
# |             2: ; ModuleID = 'LLVMDialectModule'
# | check:49'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             3: source_filename = "LLVMDialectModule"
# | check:49'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             4:
# | check:49'0     ~
# |             5: define i64 @add(i64 %0, i64 %1) {
# | check:49'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             6:  %3 = add i64 %0, %1
# | check:49'0     ~~~~~~~~~~~~~~~~~~~~~
# |             7:  ret i64 %3
# | check:49'0     ~~~~~~~~~~~~
# | check:49'1      ?           possible intended match
# |             8: }
# | check:49'0     ~~
# |             9:
# | check:49'0     ~
# |            10: !llvm.module.flags = !{!0}
# | check:49'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            11:
# | check:49'0     ~
# |            12: !0 = !{i32 2, !"Debug Info Version", i32 3}
# | check:49'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1
Failed Tests (1):
  MLIR :: CAPI/translation.c

@ftynse
Copy link
Member Author

ftynse commented Nov 23, 2023

Looks like a clash with another change that added that test.

@ftynse
Copy link
Member Author

ftynse commented Nov 23, 2023

c50972d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants