Skip to content

[C++20][Modules] Compile Error with static template function. #130057

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

Open
jaehyuck0103 opened this issue Mar 6, 2025 · 6 comments
Open

[C++20][Modules] Compile Error with static template function. #130057

jaehyuck0103 opened this issue Mar 6, 2025 · 6 comments
Labels
clang:modules C++20 modules and Clang Header Modules

Comments

@jaehyuck0103
Copy link

Hello. LLVM.

I encountered an incomprehensible compilation error related to a static template function while using C++ modules.
I provide the minimal code below to reproduce the issue.
It's my first bug report to LLVM. Please let me know if there's anything wrong.

Environment

Ubuntu clang version 21.0.0 (++20250305083430+f4878cb91612-1exp120250305083603.767)
Target: x86_64-pc-linux-gnu

Codes

// main.cpp
#include "dummy_cuda_runtime.h"
#include <cstdint>

import MyModule;

int main() {

    uint16_t *buffer;
    cudaMalloc(&buffer, 100 * sizeof(uint16_t));  // No Compile Error

    MyClass aaa;

    cudaMalloc(&buffer, 100 * sizeof(uint16_t));  // Compile Error
}
// a.cppm
module;

#include "dummy_cuda_runtime.h"
#include <cstdint>

export module MyModule;

export class MyClass {

  public:
    MyClass() {
        // If below line is not in constructor, No Compile Error
        cudaMalloc(&buffer_, 100 * sizeof(uint16_t));
    }

    uint16_t *buffer_;
};
// dummy_cuda_runtime.h
template <class T> static void cudaMalloc(T **devPtr, int size) {
    // dummy
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.30)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

project(modules-example)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_STANDARD 20)

add_executable(demo main.cpp)
target_sources(demo PUBLIC FILE_SET all_my_modules TYPE CXX_MODULES FILES
                           a.cppm)
mkdir build
cd build
cmake .. -G Ninja
ninja
FAILED: CMakeFiles/demo.dir/main.cpp.o
/usr/lib/llvm-21/bin/clang++   -std=gnu++20 -MD -MT CMakeFiles/demo.dir/main.cpp.o -MF CMakeFiles/demo.dir/main.cpp.o.d @CMakeFiles/demo.dir/main.cpp.o.modmap -o CMakeFiles/demo.dir/main.cpp.o -c /home/jae/Projects/cpp_modules3/main.cpp
/home/jae/Projects/cpp_modules3/main.cpp:15:5: error: no matching function for call to 'cudaMalloc'
   15 |     cudaMalloc(&buffer, 100 * sizeof(uint16_t)); // Compile Error
      |     ^~~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.
@frederick-vs-ja
Copy link
Contributor

This is somehow like exposing a TU-local (translation-unit-local) entity (and should be consistently rejected when compiling a.cppm if so). See also #112294.

@frederick-vs-ja frederick-vs-ja added clang:modules C++20 modules and Clang Header Modules and removed new issue labels Mar 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2025

@llvm/issue-subscribers-clang-modules

Author: Jae-Hyuck Park (jaehyuck0103)

Hello. LLVM.

I encountered an incomprehensible compilation error related to a static template function while using C++ modules.
I provide the minimal code below to reproduce the issue.
It's my first bug report to LLVM. Please let me know if there's anything wrong.

Environment

Ubuntu clang version 21.0.0 (++20250305083430+f4878cb91612-1exp120250305083603.767)
Target: x86_64-pc-linux-gnu

Codes

// main.cpp
#include "dummy_cuda_runtime.h"
#include &lt;cstdint&gt;

import MyModule;

int main() {

    uint16_t *buffer;
    cudaMalloc(&amp;buffer, 100 * sizeof(uint16_t));  // No Compile Error

    MyClass aaa;

    cudaMalloc(&amp;buffer, 100 * sizeof(uint16_t));  // Compile Error
}
// a.cppm
module;

#include "dummy_cuda_runtime.h"
#include &lt;cstdint&gt;

export module MyModule;

export class MyClass {

  public:
    MyClass() {
        // If below line is not in constructor, No Compile Error
        cudaMalloc(&amp;buffer_, 100 * sizeof(uint16_t));
    }

    uint16_t *buffer_;
};
// dummy_cuda_runtime.h
template &lt;class T&gt; static void cudaMalloc(T **devPtr, int size) {
    // dummy
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.30)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

project(modules-example)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_STANDARD 20)

add_executable(demo main.cpp)
target_sources(demo PUBLIC FILE_SET all_my_modules TYPE CXX_MODULES FILES
                           a.cppm)
mkdir build
cd build
cmake .. -G Ninja
ninja
FAILED: CMakeFiles/demo.dir/main.cpp.o
/usr/lib/llvm-21/bin/clang++   -std=gnu++20 -MD -MT CMakeFiles/demo.dir/main.cpp.o -MF CMakeFiles/demo.dir/main.cpp.o.d @<!-- -->CMakeFiles/demo.dir/main.cpp.o.modmap -o CMakeFiles/demo.dir/main.cpp.o -c /home/jae/Projects/cpp_modules3/main.cpp
/home/jae/Projects/cpp_modules3/main.cpp:15:5: error: no matching function for call to 'cudaMalloc'
   15 |     cudaMalloc(&amp;buffer, 100 * sizeof(uint16_t)); // Compile Error
      |     ^~~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.

@ChuanqiXu9
Copy link
Member

This is a bug. In main you've already included the static one. Could you test if -fmodules-reduced-bmi is helpful here?

@jaehyuck0103
Copy link
Author

@ChuanqiXu9
Good...! -fmodules-reduced-bmi removes the error.

@ChuanqiXu9
Copy link
Member

This is somehow like exposing a TU-local (translation-unit-local) entity (and should be consistently rejected when compiling a.cppm if so). See also #112294.

It didn't expose it. Since the member function in modules are not implicitly inline. So it is fine.

@ChuanqiXu9
Copy link
Member

@ChuanqiXu9 Good...! -fmodules-reduced-bmi removes the error.

OK. Reduced BMI is going to be the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules
Projects
None yet
Development

No branches or pull requests

4 participants